Relational Calculus

™ Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Relational Calculus ™ Calculus has variables, constants, comparison ops, logical connectives and quantifiers. ƒ TRC: Variables range over (i.e., get bound to) tuples. Chapter 4, Part B ƒ DRC: Variables range over domain elements (= field values). ƒ Both TRC and DRC are simple subsets of first-order logic. ™ Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Domain Relational Calculus DRC Formulas

™ ™ Query has the form: Atomic formula:     ƒ x12,x ,...,xn∈ Rname     , or X op Y, or X op constant  xx12, ,..., xnpxx | 12 , ,..., xn          ƒ op is one of <,,,,,> = ≤ ≥ ≠ ™ ™ Answer includes all tuples x 12 , x ,..., x n that Formula:   make the formula px 12, x ,..., xn be true. ƒ an atomic formula, or     ƒ ¬ pp,,∧ qp∨ q, where p and q are formulas, or ™ Formula is recursively defined, starting with ƒ ∃X (())p X , where variable X is free in p(X), or simple atomic formulas (getting tuples from relations or making comparisons of values), ƒ ∀ X (())p X , where variable X is free in p(X) and building bigger and better formulas using ™ The use of quantifiers ∃ X and ∀ X is said to bind X. the logical connectives. ƒ A variable that is not bound is free. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Free and Bound Variables Find all sailors with a rating above 7

     INTA,,, |,,, INTA∈∧> Sailors T 7  ™ The use of quantifiers ∃ X and ∀ X in a formula is   said to bind X. ™ I,,,N T A ∈Sailors ƒ A variable that is not bound is free. The condition ensures that the domain variables I, N, T and A are bound to ™ Let us revisit the definition of a query: fields of the same Sailors tuple.          xx12, ,..., xnpxx | 12 , ,..., xn      ™ The term I ,,, N T A to the left of `|’ (which should     be read as such that) says that every tuple I ,,, N T A ™ There is an important restriction: the variables that satisfies T>7 is in the answer. x1, ..., xn that appear to the left of `|’ must be the only free variables in the formula p(...). ™ Modify this query to answer: ƒ Find sailors who are older than 18 or have a rating under 9, and are called ‘Joe’. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 Find sailors rated > 7 who’ve reserved boat #103 Find sailors rated > 7 who’ve reserved a red boat

     I,,,N T A |,,,I N T A ∈∧>∧Sailors T 7  I,,,N T A |,,,I N T A ∈Sailors ∧>∧T 7  

        ∃∈∧=∧IrBrD,, IrBrD ,, Re serves Ir I Br= 103  ∃ Ir,, Br D Ir ,, Br D∈ Re serves∧=∧ Ir I          ∃ BBNC,, BBNC ,,∈ Boats∧= B Br ∧ C= ' red '   ™ We have used ∃ Ir ,, Br D () ... as a shorthand   for ∃∃Ir Br() ∃ D ()... () ™ Observe how the parentheses control the scope of each quantifier’s binding. ™ Note the use of ∃ to find a tuple in Reserves that ™ This may look cumbersome, but with a good user `joins with’ the Sailors tuple under consideration. interface, it is very intuitive. (MS Access, QBE)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Find sailors who’ve reserved all boats Find sailors who’ve reserved all boats (again!)

     I,,,N T A |,,,I N T A ∈∧Sailors  I,,,N T A |,,,I N T A ∈Sailors ∧  

      ∀ B,,BN C ∈ Boats ∀¬∈∨BBNC,,  BBNC ,, Boats                   ∃ Ir,, Br D∈ Re serves I=∧ Ir Br= B               ∃∈∧=∧IrBrD,, IrBrD ,, Re serves I Ir Br= B       

™ Find all sailors I such that for each 3-tuple BB ,, N C ™ Simpler notation, same query. (Much clearer!) either it is not a tuple in Boats or there is a tuple in ™ To find sailors who’ve reserved all red boats: Reserves showing that sailor I has reserved it.         .....  C≠'' red∨ ∃ Ir ,,Re Br D∈= serves I Ir∧ Br= B        

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Unsafe Queries, Expressive Power Summary

™ It is possible to write syntactically correct calculus queries that have an infinite number of answers! ™ Relational calculus is non-operational, and Such queries are called unsafe. users define queries in terms of what they

ƒ     want, not in terms of how to compute it. e.g.,      S| ¬∈ S Sailors        (Declarativeness.) ™ It is known that every query that can be expressed ™ Algebra and safe calculus have same in can be expressed as a safe expressive power, leading to the notion of query in DRC / TRC; the converse is also true. relational completeness. ™ Relational Completeness: (e.g., SQL) can express every query that is expressible in relational algebra/calculus. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12