Relational Calculus Relational Calculus Tuple Relational Calculus

Relational Calculus Relational Calculus Tuple Relational Calculus

∃ Relational Calculus Relational Calculus • Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). CS 186, Fall 2002, Lecture 8 • Calculus has variables, constants, comparison ops, R&G, Chapter 4 logical connectives and quantifiers. – TRC: Variables range over (i.e., get bound to) tuples. •Like SQL. ∀ – DRC: Variables range over domain elements (= field We will occasionally use this values). arrow notation unless there • Like Query-By-Example (QBE) is danger of no confusion. – Both TRC and DRC are simple subsets of first-order logic. • Expressions in the calculus are called formulas. Ronald Graham Elements of Ramsey Theory • Answer tuple is an assignment of constants to variables that make the formula evaluate to true. Tuple Relational Calculus TRC Formulas • An Atomic formula is one of the following: • Query has the form: {T | p(T)} R ∈ Rel – p(T) denotes a formula in which tuple R.a op S.b variable T appears. R.a op constant • Answer is the set of all tuples T for op is one of <>=≤≥≠,,,,, which the formula p(T)evaluates to true. • A formula can be: • Formula is recursively defined: – an atomic formula start with simple atomic formulas (get –¬∧∨p,,p q p q where p and q are formulas tuples from relations or make comparisons of ∃ values) –R(p(R)) where variable R is a tuple variable build bigger and better formulas using the –∀R(p(R)) where variable R is a tuple variable logical connectives. Free and Bound Variables Selection and Projection • Find all sailors with rating above 7 ∈ ∧ • The use of quantifiers ∃ Xand ∀ X in a formula is {S |S Sailors S.rating > 7} said to bind X in the formula. – Modify this query to answer: Find sailors who are older – A variable that is not bound is free. than 18 or have a rating under 9, and are called ‘Bob’. • Let us revisit the definition of a query: • Find names and ages of sailors with rating above 7. –{T | p(T)} {S | ∃S1 ∈Sailors(S1.rating > 7 ∧ S.sname = S1.sname • There is an important restriction ∧ S.age = S1.age)} — the variable T that appears to the left of `|’ must be the only free variable in the formula p(T). –Note, here S is a tuple variable of 2 fields (i.e. {S} is a projection of sailors), since only 2 fields are ever — in other words, all other tuple variables must be bound using a quantifier. mentioned and S is never used to range over any relations in the query. 1 Joins (continued) Joins ∈ ∧ ∧ Find sailors rated > 7 who’ve reserved boat {S | S Sailors S.rating > 7 ∃ ∈ ∧ #103 {S | SR(R∈SailorsReserves ∧ S.rating R.sid > 7 = ∧ S.sid ∧∃ ∈ ∧ ∈ ∧ ∧ ∃R(R∈ReservesB(B Boats ∧ R.sidB.bid = S.sid= R.bid {S | S Sailors S.rating > 7 ∧ ∃R(R∈Reserves ∧ R.sid = S.sid ∧ R.bidB.color = 103)} = ‘red’))} ∧ R.bid = 103)} Find sailors rated > 7 who’ve reserved boata red #103 boat Note the use of ∃ to find a tuple in Reserves • Observe how the parentheses control the scope of that `joins with’ the Sailors tuple under each quantifier’s binding. consideration. • This may look cumbersome, but it’s not so different from SQL! Division (makes more sense here???) Division – a trickier example… Find sailors who’ve reserved all boats Find sailors who’ve reserved all Red boats ∀ (hint, use ∀) {S | S∈Sailors ∧ ∀ ∈ ⇒ {S | S∈Sailors ∧ B Boats ( B.color =‘red’ ∀B∈Boats (∃R∈Reserves ∃R(R∈Reserves ∧ S.sid = R.sid (S.sid = R.sid ∧ B.bid = R.bid))} ∧ B.bid = R.bid))} Alternatively… {S | S∈Sailors ∧ • Find all sailors S such that for each tuple B in Boats ∀ ∈ ≠ ∨ there is a tuple in Reserves showing that sailor S has B Boats ( B.color ‘red’ reserved it. ∃R(R∈Reserves ∧ S.sid = R.sid ∧ B.bid = R.bid))} a ⇒ b is the same as ¬a ∨ b Unsafe Queries, Expressive Power • ∃ syntactically correct calculus queries that have an infinite number of answers! Unsafe queries. b • If a is true, b must be –e.g., ¬∈ S| S Sailors true for the implication T F to be true. If a is true and b is false, the – Solution???? Don’t do that! implication evaluates to T T F false. • Expressive Power (Theorem due to Codd): a • If a is not true, we don’t – every query that can be expressed in relational algebra care about b, the can be expressed as a safe query in DRC / TRC; the F TT expression is always converse is also true. true. • Relational Completeness: Query language (e.g., SQL) can express every query that is expressible in relational algebra/calculus. (actually, SQL is more powerful, as we will see…) 2 Summary Addendum: Use of ∀ • The relational model has rigorously defined query languages — simple and powerful. • ∀x (P(x)) - is only true if P(x) is true for • Relational algebra is more operational every x in the universe – useful as internal representation for query evaluation plans. • Relational calculus is non-operational • Usually: – users define queries in terms of what they want, not in ∀x ((x ∈ Boats) ⇒ (x.color = “Red”) terms of how to compute it. (Declarative) ⇒ • Several ways of expressing a given query • logical implication, –a query optimizer should choose the most efficient version. a ⇒ b means that if a is true, b must be • Algebra and safe calculus have same expressive power true – leads to the notion of relational completeness. a ⇒ b is the same as ¬a ∨ b Find sailors who’ve reserved all boats ... reserved all red boats {S | S∈Sailors ∧ {S | S∈Sailors ∧ ∀B( (B∈Boats) ⇒ ∀B( (B∈Boats ∧ B.color = “red”) ⇒ ∃R(R∈Reserves ∧ S.sid = R.sid ∃R(R∈Reserves ∧ S.sid = R.sid ∧ B.bid = R.bid))} ∧ B.bid = R.bid))} • Find all sailors S such that for each tuple B • Find all sailors S such that for each tuple B either it is not a tuple in Boats or there is a tuple in either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor S has reserved it. Reserves showing that sailor S has reserved it. {S | S∈Sailors ∧ {S | S∈Sailors ∧ ∀B(¬(B∈Boats) ∨ ∀B(¬(B∈Boats) ∨ (B.color ≠ “red”) ∨ ∃R(R∈Reserves ∧ S.sid = R.sid ∃R(R∈Reserves ∧ S.sid = R.sid ∧ B.bid = R.bid))} ∧ B.bid = R.bid))} 3.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us