90 likes | 311 Views
TRC vs DRC. 2003. 10. 12 한욱신. Queries in TRC and DRC. TRC Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula DRC Q = {x 1 ,x 2 ,..,x k | f(x 1 ,x 2 ,..,x k )} where x i is a (free) domain variable and f(x 1 ,x 2 ,..,x k ) is a well-formed formula.
E N D
TRC vs DRC 2003. 10. 12 한욱신
Queries in TRC and DRC • TRC • Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula • DRC • Q= {x1,x2,..,xk| f(x1,x2,..,xk)} where xi is a (free) domain variable and f(x1,x2,..,xk) is a well-formed formula
Well-formed formula • Wffs are constructed using the following rules • Each atomic formula is a wff • If P, Q are wffs, then so are NOT P, (P AND Q), (P OR Q), and (P Q) • If x is a (tuple or domain) variable and P is a wff, then so are x P, and x P
Atomic formulas in TRC and DRC • TRC • R(t) where R is a relation name and t is a tuple variable • ti[A] tj[B] where ti and tj are tuple variables; A and B are attributes names; is a comparison operator (=, <, >, , , ) • ti[A] constant • DRC • R(x1,x2,…,xk) where R is a relation name and xi is a domain variable or constant • x y where x and y are domain variables • x constant where x is a domain variable
Example Schema • Sailors(sid, sname, rating, age) • Boats(bid, bname, color) • Reserves(sid, bid, date)
Q1) Find sailors who’ve reserved at least one boat Relational Algebra sid(SailorsReserves) TRC {p|(r)(s)(Reserves(r) AND Sailors(s) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ p[1st attr] = s[sid])} /* projection */ DRC {s| (b)(d)(s2)(sn)(r)(a)(Reserves(s,b,d) AND Sailors(s2, sn, r,a) AND /* initiate tuple variables */ s = s2) /* join condition */ }
Q2) Find sid’s of sailors who’ve reserved a red or a green boat Relational Algebra TRC {p|(r)(s)(b)(Reserves(r) AND Sailors(s) AND Boats(b) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ r[bid] = b[bid] AND /* join condition */ (b[color] = ‘red’ OR b[color] = ‘green’) AND /* selection condition */ p[1st attr] = s[sname])} /* projection */ DRC {s| (b)(d)(s2)(sn)(r)(a)(b2)(bn)(c)(Reserves(s,b,d) AND Sailors(s2, sn, r,a) AND Boats(b2, bn, c) AND /* initiate tuple variables */ s = s2 AND b=b2 AND /* join condition */ (c = ‘red’ OR c = ‘green’))}
Q3) Find sid’s of sailors who’ve reserved a red and a green boat Relational Algebra TRC {p|(r)(s)(b)(Sailors(s) AND Boats(b1) AND Reserves(r1) AND Boats(b2) AND Reserves(r2) AND /* initiate tuple variables */ s[sid]=r1[sid] AND r1[bid]=b1[bid] AND s[sid]=r2[sid] AND r2[bid]=b2[bid] AND (b1[color]=‘red’ AND b2[color]=‘green’ AND p[1st attr] = s[sname])} /* projection */ DRC {s| (b)(d)(s2)(sn)(r)(a)(b2)(bn)(c) )(s2’)(sn’)(r’)(a’)(b2’)(bn’)(c’) ( Reserves(s,b,d) AND Sailors(s2, sn, r,a) AND Boats(b2, bn, c) AND Sailors(s2’, sn’, r’,a’) AND Boats(b2’, bn’, c’) AND /* initiate tuple variables */ s = s2 AND b=b2 AND s = s2’ AND b=b2’ AND/* join condition */ (c = ‘red’ AND c’ = ‘green’))}