Professional Documents
Culture Documents
1. (a) List the bars that serve a beer that Joe likes. (i) tuple calculus:
CSE 232
Fall, 2000
fb : bar j 9s 2 serves9l 2 likes (s(bar) = b(bar) ^ s(beer) = l(beer) ^ l(drinker) = Joe)g: fb : bar j 9c : beer(serves(b; c) ^ likes(c; Joe))g:
bar (serves 1 drinker=Joe(likes)):
(b) List the drinkers that frequent at least one bar that serves a beer they like. (i) tuple calculus:
fd : drinker j 9f 2 frequents9s 2 serves9l 2 likes (f (drinker) = d(drinker) ^ f (bar) = s(bar) ^s(beer) = l(beer) ^ l(drinker) = d(drinker))g
1
(c) List the drinkers that frequent only bars that serve some beer that they like. (Assume each drinker likes at least one beer and frequents at least one bar.) (i) tuple calculus:
fd : drinker j 8f 2 frequents f (drinker) = d(drinker) ! 9s 2 serves9l 2 likes(s(bar) = f (bar)^ s(beer) = l(beer) ^ l(drinker) = d(drinker))g
(ii) domain calculus: (iii) relational algebra:
drinker (frequents)
(iv) SQL:
select drinker from frequents where drinker not in (select f.drinker from frequents f where f.bar not in (select bar from serves, likes where serves.beer = likes.beer
and likes.drinker = f.drinker)) (v) QBE:
bad drinker I. d
(d) List the drinkers that frequent no bar that serves a beer that they like. This is just the complement of (b). 2. (a) List the actors cast in at least one movie by Kurosawa (here and below, K stands for Kurosawa). (i) domain calculus: (ii) relational algebra: (iii) SQL:
Also,
title;actor (film) title ( director=K (film)):
(iii) SQL:
select actor from lm where actor not in (select f.actor from lm f g where g.director = K and f.actor not in (select actor from lm where title = g.title ))
(iv) QBE:
Stage I
I.
bad-actors actor a
lm title director actor a bad-actors actor : a answer actor I. a (c) List the actors cast only in movies by Kurosawa. (i) domain calculus: (ii) relational algebra: (iii) SQL:
Stage II
(iv) QBE:
select actor from lm where actor not in (select actor from lm where director 6= K )
lm title director actor 6= K a
Stage I
I.
bad-actors actor a
lm title director actor a bad-actors actor : a answer actor I. a (d) List all pairs of actors who act together in at least one movie. (i) domain calculus:
Stage II
(iii) SQL:
I.
a1
a2
(e) List all pairs of actors cast in exactly the same movies. (i) domain calculus:
film
q1
(iii) SQL:
select f.actor, g.actor from lm f g where <f.actor, g.actor> not in Q and <g.actor, f.actor> not in Q
where Q is:
(iv) QBE:
select x.actor, y.actor from lm x y where y.actor not in (select actor from lm where title = x.title)
lm title director actor a1 t a2 t a1 : 7
Stage I
I.
lm title director actor a1 a2 bad actor1 actor2 : a1 a2 : a2 a1 answer actor1 actor2 I. a1 a2 (f) List the directors such that every actor is cast in one of his/her lms. (i) domain calculus:
Stage II
select director from lm where director not in (select f.director from lm f g where f.director not in (select director from lm where actor = g.actor ))
(iv) QBE:
Stage I
I.
bad-directors director d
lm title director actor d bad-directors director : d answer director I. d 3. (a) True. The proof uses distributivity of existential quanti cation over disjunction. (b) False (note that this says, essentially, that existential quanti ca tion distributes over conjunction). A counterexample is: 0 0 0 1 (c) False. Same counterxample as for (b). 4. One way to make the evaluation more e cient is to rewrite the query as
director title;director (movie) 1 title ( theater6="Cove" (schedule))]
Stage II
R A B
S A B
This cuts down the size of relations participating in the join and avoids building up an unnecessarily large intermediate result (resulting from the join). 5. Consider the following rspj algebra query (R is a relation with attributes ABC ):
AC AB (R) 1 BC (R)] 1 A AC (R) 1 CB (R)]:
A a a1 a a2 a
9
B b1 b1 b2 b3
C c1 c c2 c2 c
(b) There is a homomorphism of the tableau mapping the 2nd and 3rd rows to the rst. So the minimized tableau is:
A B C a b1 c1 a 1 b1 c a c
(c) An rspj algebra query corresponding to the minimized tableau is:
AC ( AB (R) 1 BC (R)):
A B C 5 8 c1 a1 8 c 5 b1 c2 a2 b2 c2 5 c
The last two rows can be mapped to the rst. The minimized tableau is:
A B C 5 8 c1 a1 8 c 5 c
A corresponding algebra query is:
AC ( A=5 B=8 (R)) 1 C ( B=8 (R)):
10
A B C a b1 c1 a 1 b c1 a 2 b c2 a 2 b2 c a b c Chasing with respect to = fAC ! B; B ! C; C ! Ag yields: A a a CHASE (T ) = a a a Eliminating duplicate rows from CHASE A B C a b c1 a b2 c a b c
and the corresponding rspj query is
AB (R) 1 AC (R):
8 b 5 CHASE (T ) = 8 b2 c
A B C
a b c
12