


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Introduction to Databases Fall-Winter 2009/
Exercises Michail Kazimianec Werner Nutt Damiano Somenzi
Note that the solutions given here are samples, i.e., there may be many more ways to express these queries in relational algebra.
Write the following queries in relational algebra.
πsname((σcolour=′red′^ (Part) 1 Catalog) 1 Supplier))
Since there is not subscript under the joins, the joins are natural joins, i.e., the common attributes are equated.
πsid(σcolour=′red′ (^) ∨ colour=′green′ (Part) 1 Catalog)
An equivalent formulation uses the union operator
πsid(σcolour=′red′ (Part) 1 Catalog ∪ σcolour=′green′ (Part) 1 Catalog).
The latter version can be refined by pushing the projection through the union:
πsid(σcolour=′red′^ (Part) 1 Catalog) ∪ πsid(σcolour=′green′^ (Part) 1 Catalog).
πsid(σcolour=′red′ (Part) 1 Catalog) ∪ πsid(σaddress=′21G.S.′ (Supplier)).
πsname(σcolour=′red′^ (Part) 1 Catalog 1 Supplier) ∪ πsname(σaddress=′21G.S.′^ (Supplier)).
Alternatively, we can pull the projection on sname to the top level.
πsname(σcolour=′red′^ (Part) 1 Catalog 1 Supplier ∪ σaddress=′21G.S.′ (Supplier)).
πsid(σcolour=′red′ (Part) 1 Catalog) ∩ πsid(σcolour=′green′ (Part) 1 Catalog)
Alternatively, we can replace the intersection with a join:
πsid(σcolour=′red′^ (Part) 1 Catalog) 1 πsid(σcolour=′green′^ (Part) 1 Catalog).
Cat1 ← Catalog Cat2 ← Catalog.
Then we join the two versions and take the projection:
πCat1.sid,Cat2.sid(Cat1 (^1) Cat1.pid=Cat2.pid ∧ Cat1.cost>Cat2.cost Cat2)
Note that we have to qualify the attributes by the relation name because both relations have attributes with the same names.
πsid(Supplier) \ πsid(Catalog 1 σcolour 6 =′red′^ (Part)).
First, we observe that the projection of Catalog on the attributes sid and pid,
πsid,pid(Catalog)
contains all pairs of suppliers and the parts they supply, expressed by sids and pids. The Cartesian product
πsid(Catalog) × πpid(Part)
contains all possible combinations of (1) the suppliers that supply something with (2) all the products. If we take the set-theoretic difference of the second and the first relation,
πsid(Catalog) × πpid(Part) \ πsid,pid(Catalog),
we obtain those combinations of suppliers and parts where the supplier does not deliver the part. Let us store the difference in the temporary relation Temp1:
Temp1 ← πsid(Catalog) × πpid(Part) \ πsid,pid(Catalog).
Now, πsid(Temp1), the projection of this relation onto the attribute sid, gives us the suppliers that do not supply some part, i.e., some part is missing in their range of products.
If there is a foreign key constraint on Catalog(sid), then the join with Supplier is superfluous. (Some query optimisers find out such redundant joins. For instance, Microsoft’s SQL Server eliminates joins that are redundant because of foreign key constraints, and it eliminates parts of the query that are inconsistent.)
πsid,sname(σcolour=′green′^ (Part) 1 σcost< 100 (Catalog) 1 Supplier))
“Find the names of suppliers supplying some red part for less than 100 Quid and some green part for less than 100 Quid.” This is the query that intuitively makes most sense.