1 / 23

Relational Calculus

Relational Calculus. Chapter 4, Part B. Relational Calculus. Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC) . Calculus has variables, constants, comparison ops , logical connectives and quantifiers .

tuan
Download Presentation

Relational Calculus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Relational Calculus Chapter 4, Part B

  2. Relational Calculus • Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus(DRC). • Calculus has variables, constants, comparison ops, logical connectives and quantifiers. • TRC: Variables range over (i.e., get bound to) tuples. • 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.

  3. Tuple Relational Calculus • Queries in tuple relational calculus : {t| (t)} • t: tuple variable • (t): well-formed formula (wff) = conditions • t is the free variable in (t) • More intuitively, {t1| cond(t1, t2, …, tn)}, where t1: free variable and t2, .., tn : bound variables • Well-formed formula • Atomic formulas connected by logical connectives, AND, OR, NOT and quantifiers, (existential quantifier), (universal quantifier)

  4. Atomic formula in TRC • R(s) • R is a relation name • s is a tuple variable • ti[A]  tj[B] •  : comparison operator (=, <, >, , , ) • ti, tj: tuple variables • A, B: attributes • ti[A]  constant

  5. Example: TRC query • Consider two relations • EMP(Name, MGR, DEPT, SAL) • CHILDREN(Ename, Cname, Age) • Q1: Retrieve Salary and Children’s name of Employees whose manager is ‘white’ {r|(e)(c)(EMP(e) AND CHILDREN(c) AND /* initiate tuple variables */ e[Name] = c[Name] AND /* join condition */ e[MGR] = ‘white’ AND /* selection cond. */ r[1st attr] = e[SAL] AND r[2nd attr] = c[Cname] } /* projection */

  6. Find the names and ages of sailors with a rating above 7 {p|(s)(Sailors(s) AND /* initiate tuple variables */ s[rating] > 7 AND /* selection condition */ p[1st attr] = s[sname] AND p[2nd attr] = s[age]) } /* projection */

  7. Find the sailor name, boat id, and reservation date for each reservation {p|(r)(s)(Reserves(r) AND Sailors(s) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ p[1st attr] = s[sname] AND p[2nd attr] = r[bid] AND p[3rd attr] = r[day] } /* projection */

  8. Find the names of sailors who have reserved boat 103 {p|(r)(s)(Reserves(r) AND Sailors(s) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ r[bid] = 103 AND /* selection condition */ p[1st attr] = s[sname] } /* projection */

  9. Find the names of sailors who have reserved a red boat {p| (r)(s)(b)(Reserves(r) AND Sailors(s) AND Boats(b) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ b[bid] = r[bid] AND /* join condition */ b[color] = ‘red’ AND /* selection condition */ p[1st attr] = s[sname] } /* projection */

  10. Find the names of sailors who have reserved at least two boats {p| (s)(r1)(r2)(Sailor(s) AND Reserves(r1) AND Reserves(r2) AND /* initiate tuple variables */ s[sid] = r1[sid] AND /* join condition */ r1[sid] = r2[sid] AND r1[bid]  r2[bid] AND /* self-join conditions */ p[1st attr] = s[sname] } /* projection */

  11. Equivalent Query in Relational Algebra • See page 115.

  12. Find sailors who have reserved all red books {s| Sailors(s) AND (b)(NOT Boats(b) OR /* initiate tuple variables */ b[color] = ‘red’  ( (r)(Reserves(r) AND r[bid] = b[bid] AND s[1st attr] = r[sid])))}/* projection */

  13. Another representation {s| Sailors(s) AND (b)(NOT Boats(b) OR /* initiate tuple variables */ b[color]  ‘red’ OR ( (r)(Reserves(r) AND r[bid] = b[bid] AND s[1st attr] = r[sid])))}/* projection */

  14. Domain Relational Calculus • Queries in domain relational calculus : {x1, x2, ..., xk| (t)} • x1, x2, …, xk: domain variables; these are only free variables in (t) • (t): well-formed formula (wff) = conditions • Well-formed formula • Atomic formulas connected by logical connectives, AND, OR, NOT and quantifiers, (existential quantifier), (universal quantifier)

  15. Atomic formula in DRC • R(x1, x2, …, xk) • R is a k-ary relation • xi : domain variable or constant • x  y •  : comparison operator (=, <, >, , , ) • x, y: domain variables • A, B: attributes • x  constant

  16. DRC Examples • Consider two relations • EMP(Name, MGR, DEPT, SAL) • CHILDREN(Ename, Cname, Age) • Q1: Retrieve Salary and Children’s name of Employees whose manager is ‘white’ {q,s|(u)(v)(w)(x)(y)(EMP(u,v,w,q) AND CHILDREN(x,s,y) AND /* initiate domain variables */ u = x AND /* join condition */ v = ‘white’ } /* selection condition */ /* projection is implied (q, s) */

  17. Find all sailors with a rating above 7 {i,n,t,a| Sailors(i,n,t,a) AND t > 7}

  18. Find sailors rated > 7 who’ve reserved boat #103 {i,n,t,a|(ir)(br)(d) (Sailors(i,n,t,a) AND Reserve(ir,br,d) AND ir = i AND /* join condition */ t > 7 AND br = 103)} /* selection condition */

  19. Find sailors rated > 7 who’ve reserved a red boat {i,n,t,a| (ir)(br)(d) (Sailors(i,n,t,a) AND (Reserve(ir,br,d) AND t>7 AND (b)(bn)(c)(Boats(b,bn,c) AND b = br AND c=‘red’) )}

  20. Find sailors who’ve reserved all boats • {i,n,t,a|Sailers(i,n,t,a) AND (b)(bn)(c)(NOT Boats(b,bn,c) OR (ir) (br)(d)(Reserves(ir,br,d) AND i = ir AND br = b) ) )}

  21. Unsafe Queries, Expressive Power • It is possible to write syntactically correct calculus queries that have an infinite number of answers! Such queries are called unsafe. • e.g., • It is known that every query that can be expressed in relational algebra can be expressed as a safe query in DRC / TRC; the converse is also true. • Relational Completeness: Query language (e.g., SQL) can express every query that is expressible in relational algebra/calculus.

  22. Summary • Relational calculus is non-operational, and users define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.) • Algebra and safe calculus have same expressive power, leading to the notion of relational completeness.

  23. Exercises • 4.3, 4.6

More Related