Docsity

Relational Calculus - Databases | COP 4710, Assignments of Computer Science

Partial preview of the text

Download Relational Calculus - Databases | COP 4710 and more Assignments Computer Science in PDF only on Docsity!

Relational Calculus

R&G, Chapter 4

Relational Calculus

  • Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC).
  • Calculus hasvariables, constants, comparison ops, logical connectives and quantifiers. - TRC: Variables range over (i.e., get bound to) tuples. - Like SQL. - DRC: Variables range over domain elements (= field values). - Like Query-By-Example (QBE) - Both TRC and DRC are simple subsets of first-order logic. - We’ll focus on TRC here
  • Expressions in the calculus are called formulas.
  • Answer tuple is an assignment of constants to variables that make the formula evaluate to true.

TRC Formulas

  • An Atomic formula is one of the following: R  Rel R.a op S.b R.a op constant op is one of
  • A formula can be:
    • an atomic formula
    • where p and q are formulas
    • where variable R is a tuple variable
    • where variable R is a tuple variable      , , , , ,  p p ,  q p ,  qR ( p ( R ))  R ( p ( R ))

Free and Bound Variables

  • The use of quantifiers and in a formula is said to bind X in the formula. - A variable that is not bound is free.
  • Let us revisit the definition of a query:
    • {T | p(T)}  XX
  • There is an important restriction — the variable T that appears to the left of `|’ must be the only free variable in the formula p(T). — in other words, all other tuple variables must be bound using a quantifier.

Find sailors rated > 7 who’ve reserved boat # Note the use of  to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration.

{S | SSailors  S.rating > 7 

R(RReserves  R.sid = S.sid

 R.bid = 103)}

Joins

Joins (continued)

  • Observe how the parentheses control the scope of each quantifier’s binding.
  • This may look cumbersome, but it’s not so different from SQL!

{S | SSailors  S.rating > 7 

R(RReserves  R.sid = S.sid

 R.bid = 103)}

{S | SSailors  S.rating > 7 

R(RReserves  R.sid = S.sid

 B(BBoats  B.bid = R.bid

 B.color = „red‟))}

Find sailors rated > 7 who’ve reserved boat # Find sailors rated > 7 who’ve reserved a red boat

Division – a trickier example…

{S | SSailors 

B  Boats ( B.color = ‘red’ 

R(RReserves  S.sid = R.sid

 B.bid = R.bid))}

Find sailors who‟ve reserved all Red boats

{S | SSailors 

B  Boats ( B.color  ‘red’ 

R(RReserves  S.sid = R.sid

 B.bid = R.bid))}

Alternatively…

a  b is the same as a  b

  • If a is true, b must be true! - If a is true and b is false, the implication evaluates to false.
  • If a is not true, we don’t care about b - The expression is always true.

a

T

F

T F

b

T

T T

F

Summary

  • The relational model has rigorously defined query languages — simple and powerful.
  • Relational algebra is more operational
    • useful as internal representation for query evaluation plans.
  • Relational calculus is non-operational
    • users define queries in terms of what they want, not in terms of how to compute it. (Declarative)
  • Several ways of expressing a given query
    • a query optimizer should choose the most efficient version.
  • Algebra and safe calculus have same expressive power
    • leads to the notion of relational completeness.

Addendum: Use of 

  • x (P(x)) - is only true if P(x) is true for every x in the universe
  • Usually:  x ((xBoats)(x.color = “Red”)
  •  logical implication, ab means that if a is true, b must be true ab is the same as a  b

... reserved all red boats

  • Find all sailors S such that for each tuple B either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor S has reserved it. {S | SSailors  B( (BBoats  B.color = “red”)  R(RReserves  S.sid = R.sid  B.bid = R.bid))} {S | SSailors  B((BBoats)  (B.color  “red”)  R(RReserves  S.sid = R.sid  B.bid = R.bid))}