Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.

Similar presentations


Presentation on theme: "1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B."— Presentation transcript:

1 1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B

2 2 Copyright © Kyu-Young Whang 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 3 Copyright © Kyu-Young Whang 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, {t 1 | cond(t 1, t 2, …, t n )}, where t 1 : free variable and t 2,.., t n : bound variables  Well-formed formula  Atomic formulas connected by logical connectives, AND, OR, NOT and quantifiers,  (existential quantifier),  (universal quantifier)

4 4 Copyright © Kyu-Young Whang Atomic formula in TRC 1. R(s)  R is a relation name  s is a tuple variable 2. t i [A]  t j [B]   : comparison operator (=,, , ,  )  t i, t j : tuple variables  A, B: attributes 3. t i [A]  constant

5 5 Copyright © Kyu-Young Whang 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[1 st attr] = e[SAL] AND r[2 nd attr] = c[Cname] }/* projection */

6 6 Copyright © Kyu-Young Whang 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[1 st attr] = s[sname] AND p[2 nd attr] = s[age]) }/* projection */

7 7 Copyright © Kyu-Young Whang 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[1 st attr] = s[sname] AND p[2 nd attr] = r[bid] AND p[3 rd attr] = r[day] }/* projection */

8 8 Copyright © Kyu-Young Whang 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[1 st attr] = s[sname] }/* projection */

9 9 Copyright © Kyu-Young Whang 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[1 st attr] = s[sname] }/* projection */

10 10 Copyright © Kyu-Young Whang 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[1 st attr] = s[sname] }/* projection */

11 11 Copyright © Kyu-Young Whang Equivalent Query in Relational Algebra  See page 115.

12 12 Copyright © Kyu-Young Whang 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[1 st attr] = r[sid])))}/* projection */

13 13 Copyright © Kyu-Young Whang 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[1 st attr] = r[sid])))}/* projection */

14 14 Copyright © Kyu-Young Whang Domain Relational Calculus  Queries in domain relational calculus : {x 1, x 2,..., x k |  (t)}  x 1, x 2, …, x k : 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 15 Copyright © Kyu-Young Whang Atomic formula in DRC  R(x 1, x 2, …, x k )  R is a k-ary relation  x i : domain variable or constant  x  y   : comparison operator (=,, , ,  )  x, y: domain variables  A, B: attributes  x  constant

16 16 Copyright © Kyu-Young Whang 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 17 Copyright © Kyu-Young Whang Find all sailors with a rating above 7 {i,n,t,a| Sailors(i,n,t,a) AND t > 7}

18 18 Copyright © Kyu-Young Whang 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 19 Copyright © Kyu-Young Whang 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 20 Copyright © Kyu-Young Whang 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 21 Copyright © Kyu-Young Whang 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 22 Copyright © Kyu-Young Whang 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 23 Copyright © Kyu-Young Whang Exercises  4.3, 4.6


Download ppt "1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B."

Similar presentations


Ads by Google