Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480: Database Systems Lecture 15 February 18, 2013.

Similar presentations


Presentation on theme: "CS 480: Database Systems Lecture 15 February 18, 2013."— Presentation transcript:

1 CS 480: Database Systems Lecture 15 February 18, 2013

2 Example { x |  y  order ((y[buyer_name] = ‘Jewel’)   z  supplies
ORDER(order_id,buyer_name,item,quantity), SUPPLIES(supp_name,item) Query: Retrieve names of all the suppliers that supply all the items that ‘Jewel’ ordered. { x |  y  order ((y[buyer_name] = ‘Jewel’)   z  supplies (z[supp_name] = x[supp_name]  y[item] = z[item] )) }

3 Example { x |  y  order ((y[buyer_name] = ‘Jewel’)   z  supplies
ORDER(order_id,buyer_name,item,quantity), SUPPLIES(supp_name,item) Query: Retrieve names of all the suppliers that supply all the items that ‘Jewel’ ordered. { x |  y  order ((y[buyer_name] = ‘Jewel’)   z  supplies (z[supp_name] = x[supp_name]  y[item] = z[item] )) }

4 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { t |  d  drinker ( d[d_name] = t[d_name]   s  serves ( s[be_name] = ‘Bud’   f  frequents ( f [ba_name] = s[ba_name]  f [d_name] = t[d_name])))}

5 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { t |  d  drinker ( d[d_name] = t[d_name]   s  serves ( s[be_name] = ‘Bud’   f  frequents ( f [ba_name] = s[ba_name]  f [d_name] = t[d_name])))} If the bolded formula is untrue, it doesn’t matter. That’s exactly what we want.

6 Domain Relational Calculus (DRC)
Similar to tuple relational calculus, except that each variable is defined on an attribute, rather than a tuple. An expression in the DRC is of the form: { < x1,x2,…,xn > | P(x1,x2,…,xn) } x1,x2,…,xn are domain variables. P is a formula composed of atoms (as in TRC).

7 Example #1 student1(STUDENT) student2(STUDENT)
STUDENT = {id,name,major,GPA} RA Query: student1  student2 Domain Relational Calculus: { < i,n,m,g > | < i,n,m,g >  student1  < i,n,m,g >  student2 }

8 Example #2 student(STUDENT) STUDENT = {id,name,major,GPA}
RA Query: Πid(student) Domain Relational Calculus: “ n,m,g” is shorthand for  n ( m ( g (…))) { < i > |  n,m,g ( < i,n,m,g >  student )}

9 Example #3 student(STUDENT), enrol(ENROL)
STUDENT = {id,name,major,GPA} ENROL = {id,course,grade} RA Query: Πname,grade(student enrol) Domain Relational Calculus: { < n,g2 > |  i,m,g1,c (< i,n,m,g1 >  student  < i,c,g2 >  enrol }

10 { < x1,x2,…,xn > | P(x1,x2,…,xn) }
Formal Definition A domain-relational-calculus expression is of the form: { < x1,x2,…,xn > | P(x1,x2,…,xn) } Where P(x1,x2,…,xn) is a formula (predicate) in which x1,x2,…,xn are free (unbound) domain variables. A free variable will be one that is not quantified by  or  . Go back one slide to show which are the free variables and which are the bound variables in the previous example.

11 Formal Definition Let x1,x2,…,xn be domain variables, r a relation. Then, the following are atoms: < x1,x2,…,xn >  r xi θ xj, where θ is a comparison operator xi θ c, where c is a constant An atom is a formula.

12 Formal Definition Same as with TRC:
If P1 and P2 are formulas, then the following are also formulas: (P1), P1 P1  P2, P1  P2, P1  P2 If P1(s) contains a free domain variable s and r is a relation then the following are also formulas:  s (P1(s))  s (P1(s)) There exists s such that P1 is true For all s, P1 is true.

13 Beer Drinking Example #1
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which bars serve some beer that ‘John Doe’ likes? { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

14 Beer Drinking Example #1
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which bars serve some beer that ‘John Doe’ likes? { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

15 Beer Drinking Example #1
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which bars serve some beer that ‘John Doe’ likes? { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

16 Beer Drinking Example #1
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which bars serve some beer that ‘John Doe’ likes? { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

17 Beer Drinking Example #1
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which bars serve some beer that ‘John Doe’ likes? { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

18 Beer Drinking Example #2
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent at least one bar that serves some beer they like? { < d > |  a, e ( < a,e >  serves  < d,e >  likes  < d,a >  frequents ) }

19 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { < d > |  c ( < d,c >  drinker   a,e ( ( < a,e >  serves  e = ‘Bud’ )  < d,a >  frequents ) } I don’t need the  c…

20 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { < d > |  c ( < d,c >  drinker   a,e ( ( < a,e >  serves  e = ‘Bud’ )  < d,a >  frequents ) ) } We do need the  c because if it wasn’t there then if no bar served Bud then the query would return no drinkers which would be a mistake.

21 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { < d > |  c ( < d,c >  drinker   a,e ( ( < a,e >  serves  e = ‘Bud’ )  < d,a >  frequents ) ) } I don’t need the  c…

22 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { < d > |  c ( < d,c >  drinker   a,e ( ( < a,e >  serves  e = ‘Bud’ )  < d,a >  frequents ) ) } I don’t need the  c…

23 Beer Drinking Example #3
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent every bar that serves ‘Bud’? { < d > |  c ( < d,c >  drinker   a,e ( ( < a,e >  serves  e = ‘Bud’ )  < d,a >  frequents ) ) } I don’t need the  c…

24 Beer Drinking Example #4
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent only bars that serve some beer they like? { < d > |  c ( < d,c >  drinker   a ( < d,a >  frequents   e ( < d,e >  likes  < a,e >  serves ) ) ) }

25 Beer Drinking Example #4
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent only bars that serve some beer they like? { < d > |  c ( < d,c >  drinker   a ( < d,a >  frequents   e ( < d,e >  likes  < a,e >  serves ) ) ) }

26 Beer Drinking Example #4
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent only bars that serve some beer they like? { < d > |  c ( < d,c >  drinker   a ( < d,a >  frequents   e ( < d,e >  likes  < a,e >  serves ) ) ) }

27 Beer Drinking Example #4
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent only bars that serve some beer they like? { < d > |  c ( < d,c >  drinker   a ( < d,a >  frequents   e ( < d,e >  likes  < a,e >  serves ) ) ) }

28 Beer Drinking Example #4
Relational Schema: DRINKER(d_name,d_city) BAR(ba_name,ba_city) BEER(be_name,type) FREQUENTS(d_name,ba_name) SERVES(ba_name,be_name) LIKES(d_name,be_name) Query: Which drinkers frequent only bars that serve some beer they like? { < d > |  c ( < d,c >  drinker   a ( < d,a >  frequents   e ( < d,e >  likes  < a,e >  serves ) ) ) }


Download ppt "CS 480: Database Systems Lecture 15 February 18, 2013."

Similar presentations


Ads by Google