Formal Query Languages
Formal Query Languages
Set Theory
• In mathematics set theory is one of the most fundamental concepts,
and its formalization was a major event in the history of mathematics.
• Set theory is a foundation for most mathematical disciplines.
• A set is a collection of distinct elements considered as a whole, and
element membership of a set is the fundamental concept.
• Sets can be represented in different ways:
• As a list of individual elements: {e1, e2, e3}
Ordering and repetitions have no importance, e.g.
{e1, e2, e3} = {e2, e1, e3} = {e1, e2, e2, e3}
• By a description of element properties: {e ϵ Nat | e < 4 v e = 9} (= {0, 1, 2, 3, 9})
in such a way, that it can be determined whether a given element e is a member of
the set or not.
• Set operations: Union, Difference, Intersection, Cartesian Product …
Algebra
• A fundamental discipline in mathematics that arose from the idea
that one can perform arithmetic operations on non numerical objects
called variables. (Arithmetic: 3+4 = 4+3 ; Algebra: X+Y = Y+X).
• In its most general form, algebra is the study of mathematical
symbols and the rules for manipulating these symbols.
Logic
• Propositional Calculus is a formal system, where formulas are
propositions over some variables, like P(x), which for each possible
value of x has a truth value of either True or False. The formulas may
contain sub-formulas combined by logical operators like /\ (“and”), \/
(“or”), and => (“implies”).
Example:
P(x) ≜ x > 7 /\ x < 10; P(x) is True for x = 8 and for x = 9, and False for all other values
of x.
• Predicate Calculus extends propositional calculus by allowing formulas
to be quantified with the Existential Quantifier or the Universal
Quantifier:
∃x(P(x)) Reads: There exists some x for which P(x) is True
∀x(P(x)) Reads: For all x the proposition P(x) is True
Basic Operations
• Is a minimal set of operations.
Derived Operations
• Additional operations that can be derived from the Basic operations, not for
achieving more expressive power, but for expressive convenience.
Extended Operations
• Give more expressive power - can’t be derived from the Basic operations.
Selection P(A1,…, An)(R) Those tuples t of R, for which P(t) is True: { t | t R P(t) }
Projection Ai, …, Aj (R) Delete columns not listed in Ai, …, Aj, then delete duplicate tuples:
{ (Ai, …, Aj) | (A1,…, An) R}
Set Union RS The union of tuples of R and S: { t | t R v t S }
Only defined when R and S are compatible.
Set Difference R–S Those tuples of R that are not in S: { t | t R t S}
Only defined when R and S are compatible.
Cartesian Product RxS All combined tuples: { (r1,…, rn, s1,…, sm) | (r1,…, rn) R (s1,…, sm) S }
Rename S(R) Rename of R to S.
S(L2)(R(L1)) Rename R to S and rename attributes in L1 to attributes in L2
• Compatible means that R and S must have the same number of attributes with
matching, or at least compatible, domains.
• Note: Selection corresponds to SQL’s where clause (and not SQL’s select
clause).
Left Outer Join R⟕S SELECT * FROM R NATURAL LEFT OUTER JOIN S
Right Outer Join R⟖S SELECT * FROM R NATURAL RIGHT OUTER JOIN S
Full Outer Join R⟗S SELECT * FROM R NATURAL FULL OUTER JOIN S
Set Intersection RS (SELECT * FROM R) INTERSECT (SELECT * FROM S)
Assignment SR CREATE TABLE S SELECT * FROM R
E1, E2, …, En are arithmetic expressions involving constants and attribute names of R.
AVG, MIN, MAX, SUM and COUNT are aggregate functions taking a multiset of values as input
and returning a single value.
Fi is an aggregate function, and Ai is an attribute name of R.
AX is a list of attributes on which to group; Can be empty.
• Note for a query of the form {t | … tR … } all attributes of R will be kept in t.
Otherwise a projection to those attributes A for which t[A] are defined will be
made.
Readings
• In Database Systems Concepts please read Chapter 6.
(Remember to check the book typo list – there are several typos.)
• Pay special attention to the Summary
Make the
Demo Exercises.
d) Domain Calculus:
{<n,i> | d,s(<i,n,d,s>Instructor
d=‘Physics’ s>90000)}