Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480: Database Systems Lecture 9 February 4, 2013.

Similar presentations


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

1 CS 480: Database Systems Lecture 9 February 4, 2013

2 Examples #3 d-name d-city ba-name ba-city Frequents Drinker Bar Likes
Serves All of them are many to many. Beer be-name type

3 Examples #3 d-name d-city ba-name ba-city Frequents Drinker Bar Likes
Serves Relational Schema: DRINKER(d-name,d-city) BAR(ba-name,ba-city) BEER(be-name,type) One relation for each entity set Beer be-name type

4 Examples #3 d-name d-city ba-name ba-city Frequents Drinker Bar Likes
Serves 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) One relation for each relationship set Beer be-name type

5 Example #3, Query #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 #1: Which bars serve a beer that Joe likes?

6 Example #3, Query #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 #1: Which bars serve a beer that Joe likes? Steps to take? Retrieve the beers that Joe likes. Find out which bars serve each of those beers. Extract the bar names for those bars.

7 Example #3, Query #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 #1: Which bars serve a beer that Joe likes? Steps to take? Retrieve the beers that Joe likes. Find out which bars serve each of those beers. Extract the bar names for those bars.

8 Example #3, Query #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 #1: Which bars serve a beer that Joe likes? Steps to take? Retrieve the beers that Joe likes. Find out which bars serve each of those beers. Extract the bar names for those bars.

9 Example #3, Query #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 #1: Which bars serve a beer that Joe likes? Steps to take? Retrieve the beers that Joe likes. Find out which bars serve each of those beers. Extract the bar names for those bars.

10 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Step 1: Retrieve the beers that Joe likes. Πba-name(σd-name=‘Joe’(likes) serves)

11 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Step 1: Retrieve the beers that Joe likes. Πba-name(σd-name=‘Joe’(likes) serves)

12 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Step 2: Find out which bars serve each of those beers. Πba-name(σd-name=‘Joe’(likes) serves)

13 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Step 2: Find out which bars serve each of those beers. Πba-name(σd-name=‘Joe’(likes) serves)

14 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Step 3: Extract the bar names for those bars. Πba-name(σd-name=‘Joe’(likes) serves)

15 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Step 3: Extract the bar names for those bars. Πba-name(σd-name=‘Joe’(likes) serves)

16 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes) serves)
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 #1: Which bars serve a beer that Joe likes? Πba-name(σd-name=‘Joe’(likes) serves)

17 Example #3, Query #1 Πba-name(σd-name=‘Joe’(likes serves))
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 #1: Which bars serve a beer that Joe likes? Are these two queries equivalent? Πba-name(σd-name=‘Joe’(likes serves)) Πba-name(σd-name=‘Joe’(likes) serves)

18 Example #3, Query #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 #2: Which drinkers frequent all the bars that serve ‘Bud’?

19 Example #3, Query #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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Steps to take? Compute all bars that serve Bud Find out all drinkers that frequent all of those

20 Example #3, Query #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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Steps to take? Compute all bars that serve Bud Find out all drinkers that frequent all of those

21 Example #3, Query #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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Steps to take? Compute all bars that serve Bud Find out all drinkers that frequent all of those

22 Example #3, Query #2 frequents  Πba-name(σbe-name=‘Bud’(serves))
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Step 1: Compute all bars that serve Bud frequents  Πba-name(σbe-name=‘Bud’(serves))

23 Example #3, Query #2 frequents  Πba-name(σbe-name=‘Bud’(serves))
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Step 1: Compute all bars that serve Bud frequents  Πba-name(σbe-name=‘Bud’(serves))

24 Example #3, Query #2 frequents  Πba-name(σbe-name=‘Bud’(serves))
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Step 1: Compute all bars that serve Bud frequents  Πba-name(σbe-name=‘Bud’(serves))

25 Example #3, Query #2 frequents  Πba-name(σbe-name=‘Bud’(serves))
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Step 2: Find out all drinkers that frequent all of those frequents  Πba-name(σbe-name=‘Bud’(serves))

26 Example #3, Query #2 frequents  Πba-name(σbe-name=‘Bud’(serves))
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Step 2: Find out all drinkers that frequent all of those frequents  Πba-name(σbe-name=‘Bud’(serves))

27 Example #3, Query #2 frequents  Πba-name(σbe-name=‘Bud’(serves))
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? frequents  Πba-name(σbe-name=‘Bud’(serves))

28 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves)))

29 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves)))

30 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves))) All possible drinker,bar pairs

31 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves)))

32 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves))) Drinkers with the bars that they don’t frequent.

33 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves)))

34 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves))) Drinkers with the bars that serve Bud that they don’t frequent.

35 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves)))

36 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves))) Drinkers that don’t frequent some bar that serves Bud.

37 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves))) Drinkers that frequent all bars that serve ‘Bud’.

38 Example #3, Query #2 Πd-name(frequents) – Πd-name (
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 #2: Which drinkers frequent all the bars that serve ‘Bud’? Middle line is pairs of (d-name,ba-name) such that the drinker does not frequent that bar. Join it with bottom line. Bars that serve Bud… Then you have drinkers that don’t frequent some bar that serves bud. Take out all of those with the top line and you get only drinkers that go to all bars that serve Bud. Alternative without division: Πd-name(frequents) – Πd-name ( ((Πd-name(drinker)  Πba-name(bar)) – frequents) Πba-name(σbe-name=‘Bud’(serves)))

39 Example #3, Query #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 #3: Which drinkers frequent a bar that serves a beer they like?

40 Example #3, Query #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 #3: Which drinkers frequent a bar that serves a beer they like? Steps to take? Get drinkers with the beers they like and the bars serve those beers. Of those, also get only the bars that they also frequent. Extract the drinkers.

41 Example #3, Query #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 #3: Which drinkers frequent a bar that serves a beer they like? Steps to take? Get drinkers with the beers they like and the bars serve those beers. Of those, also get only the bars that they also frequent. Extract the drinkers.

42 Example #3, Query #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 #3: Which drinkers frequent a bar that serves a beer they like? Steps to take? Get drinkers with the beers they like and the bars serve those beers. Of those, also get only the bars that they also frequent. Extract the drinkers.

43 Example #3, Query #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 #3: Which drinkers frequent a bar that serves a beer they like? Steps to take? Get drinkers with the beers they like and the bars that serve those beers. Of those, also get only the bars that they also frequent. Extract the drinkers.

44 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Step 1: Get drinkers with the beers they like and the bars that serve those beers. Πd-name(likes serves frequents)

45 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Step 1: Get drinkers with the beers they like and the bars that serve those beers. Πd-name(likes serves frequents)

46 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Step 2: Of those, also get only the bars that they also frequent. Πd-name(likes serves frequents)

47 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Step 2: Of those, also get only the bars that they also frequent. Πd-name(likes serves frequents)

48 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Step 3: Extract the drinkers. Πd-name(likes serves frequents)

49 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Step 3: Extract the drinkers. Πd-name(likes serves frequents)

50 Example #3, Query #3 Πd-name(likes serves frequents)
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 #3: Which drinkers frequent a bar that serves a beer they like? First join is drinker,beer,bar such that drinker likes a beer and the bar serves that beer. Second join strips out the triples that had a bar that the drinker does not frequent. Πd-name(likes serves frequents)

51 Example #3, Query #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 #4: Which drinkers don’t frequent any bar that serves a beer they like? All drinkers except the ones that frequent a bar that serves a beer they like. Then it’s the opposite of query #3.

52 Example #3, Query #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 #4: Which drinkers don’t frequent any bar that serves a beer they like? All drinkers except the ones that frequent a bar that serves a beer they like. Then it’s the opposite of query #3. Steps to take? Compute the drinkers that do frequent a bar that serves a beer they like. Compute the opposite of those.

53 Example #3, Query #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 #4: Which drinkers don’t frequent any bar that serves a beer they like? All drinkers except the ones that frequent a bar that serves a beer they like. Then it’s the opposite of query #3. Steps to take? Compute the drinkers that do frequent a bar that serves a beer they like. Compute the opposite of those.

54 Example #3, Query #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 #4: Which drinkers don’t frequent any bar that serves a beer they like? All drinkers except the ones that frequent a bar that serves a beer they like. Then it’s the opposite of query #3. Steps to take? Compute the drinkers that do frequent a bar that serves a beer they like. Compute the opposite of those.

55 Example #3, Query #4 Πd-name(drinker) –
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 #4: Which drinkers don’t frequent any bar that serves a beer they like? Step 1: Compute the drinkers that do frequent a bar that serves a beer they like. Πd-name(drinker) – Πd-name(likes serves frequents)

56 Example #3, Query #4 Πd-name(drinker) –
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 #4: Which drinkers don’t frequent any bar that serves a beer they like? Step 1: Compute the drinkers that do frequent a bar that serves a beer they like. Πd-name(drinker) – Πd-name(likes serves frequents)

57 Example #3, Query #4 Πd-name(drinker) –
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 #4: Which drinkers don’t frequent any bar that serves a beer they like? Step 2: Compute the opposite of those. Πd-name(drinker) – Πd-name(likes serves frequents)

58 Example #3, Query #4 Πd-name(drinker) –
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 #4: Which drinkers don’t frequent any bar that serves a beer they like? Step 2: Compute the opposite of those. Πd-name(drinker) – Πd-name(likes serves frequents)

59 Example #3, Query #4 Πd-name(drinker) –
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 #4: Which drinkers don’t frequent any bar that serves a beer they like? Πd-name(drinker) – Πd-name(likes serves frequents)

60 Example #3, Query #5 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 #5: Which drinkers frequent only bars that serve some beer they like? All drinkers except the ones that frequent some bar that doesn’t serve any beer they like.

61 Example #3, Query #5 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 #5: Which drinkers frequent only bars that serve some beer they like? All drinkers except the ones that frequent some bar that doesn’t serve any beer they like. Steps to take? Compute drinkers with bars that serve a beer they like. Take those out of frequents (gives us drinkers that frequent a bar that don’t serve a beer they like). Take those out of drinker.

62 Example #3, Query #5 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 #5: Which drinkers frequent only bars that serve some beer they like? Opposite of: Drinkers that frequent some bar that don’t serve a beer they like. All drinkers except the ones that frequent some bar that doesn’t serve any beer they like. Steps to take? Compute drinkers with bars that serve a beer they like. Take those out of frequents (gives us drinkers that frequent a bar that don’t serve a beer they like). Take those out of drinker.


Download ppt "CS 480: Database Systems Lecture 9 February 4, 2013."

Similar presentations


Ads by Google