250 likes | 607 Views
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 .
E N D
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. • 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.
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)
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
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 */
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 */
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 */
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 */
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 */
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 */
Equivalent Query in Relational Algebra • See page 115.
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 */
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 */
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)
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
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) */
Find all sailors with a rating above 7 {i,n,t,a| Sailors(i,n,t,a) AND t > 7}
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 */
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’) )}
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) ) )}
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.
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.
Exercises • 4.3, 4.6