1 copyright © kyu-young whang relational calculus chapter 4, part b

23
opyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B

Upload: bryan-richardson

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

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

1Copyright © Kyu-Young Whang

Relational Calculus

Chapter 4, Part B

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

2Copyright © 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.

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

3Copyright © 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,{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)

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

4Copyright © Kyu-Young Whang

Atomic formula in TRC

1. R(s) R is a relation name s is a tuple variable

2. ti[A] tj[B] : comparison operator (=, <, >, , , ) ti, tj: tuple variables A, B: attributes

3. ti[A] constant

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

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

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

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

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

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

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

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

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

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

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

10Copyright © 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] ANDr1[bid] r2[bid] AND /* self-join conditions */p[1st attr] = s[sname] } /* projection */

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

11Copyright © Kyu-Young Whang

Equivalent Query in Relational Algebra

See page 115.

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

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

*/

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

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

*/

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

14Copyright © Kyu-Young Whang

Domain Relational Calculus

Queries in domain relational calculus : {x1, x2, ..., x

k| (t)} x1, x2, …, xk: domain variables; these are only free varia

bles 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)

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

15Copyright © Kyu-Young Whang

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

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

16Copyright © 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) */

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

17Copyright © Kyu-Young Whang

Find all sailors with a rating above 7

{i,n,t,a| Sailors(i,n,t,a) AND t > 7}

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

18Copyright © 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 */

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

19Copyright © 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) ANDt>7 AND

(b)(bn)(c)(Boats(b,bn,c) AND b = br AND c=‘red’)

)}

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

20Copyright © 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)

))}

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

21Copyright © 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.

S S Sailors|

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

22Copyright © 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.

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

23Copyright © Kyu-Young Whang

Exercises

4.3, 4.6