I\'m following the Stanford Database course and there\'s a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Re
Definitely this is the concept of division operator in relational algebra.
But I tried on that course. The RA Relational Algebra Syntax doesn't support dev operator. So I used diff and cross instead. Here is my solution:
\project_{pizzeria}(Serves)
\diff
\project_{pizzeria}(
(\project_{pizzeria}(Serves)
\cross
\project_{pizza}(\project_{name}(\select_{age>30}(Person))\join Eats))
\diff
\project_{pizzeria,pizza}(Serves)
)
Based on the assumption that all pizzerias serve at least one type of pizza, we will find that the group of pizzas that people over 30 do NOT EAT will be sold by all the pizzerias EXCEPT the one(s) who sell exclusively pizzas which people over 30 do EAT. Did it help?
Try doing a join using conditions rather than a cross. The conditions would be sure that you match up the records correctly (you only include them if they are in both relations) rather than matching every record in the first relation to every record in the second relation.
\project_{pizzeria}Serves
\diff
\project_{pizzeria}((\project_{pizza}(\select_{age < 30}Person\joinEats)
\diff\project_{pizza}(\select_{age > 30}Person\joinEats))\joinServes);
========================================================================
it's as simple as that. What did I do? in the second part I found pizza list that did not include pizzas that are eaten by those above 30.
I joined them with pizzerias in order to see which pizzerias make pizza for younger people also.
I differed that from the original list of pizzerias and the only one that makes pizza for those above 30 is Chicago Pizza.
The solution is the join div operator http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29
See http://oracletoday.blogspot.com/2008/04/relational-algebra-division-in-sql.html
On slide 6, note that n is (3 1 7)
.
On the next slide, o / n
results in (4 8)
.
If o
would also have (12 3)
and (12 1)
but not (12 7)
, 12 would not be part of o / n
.
You should be able to fill in an example in the formula on Slide 16 and work it out.
In your case, we take ɑ
to be:
Chicago Pizza cheese cheese
Chicago Pizza cheese supreme
Chicago Pizza supreme cheese
Chicago Pizza supreme supreme
Dominos cheese cheese
Dominos cheese supreme
Then we take β
to be:
cheese cheese
cheese supreme
supreme cheese
supreme supreme
The result of ɑ / β
would then be:
Chicago Pizza
Dominos
is not part of this because it misses (supreme cheese)
and (supreme supreme)
.