Docsity

Relational Algebra: Sample Solutions, Study notes of Algebra

Toggle sidebar
bg1
Introduction to Databases Fall-Winter 2009/10
Exercises Michail Kazimianec
Werner Nutt
Damiano Somenzi
Relational Algebra: Sample Solutions
Note that the solutions given here are samples, i.e., there may be many more ways to
express these queries in relational algebra.
1. Write queries in relational algebra
Write the following queries in relational algebra.
1. “Find the names of suppliers who supply some red part.”
πsname((σcolour=0red0(Part)1Catalog)1Supplier))
Since there is not subscript under the joins, the joins are natural joins, i.e., the
common attributes are equated.
2. “Find the IDs of suppliers who supply some red or green part.”
πsid(σcolour=0red0∨colour=0green0(Part)1Catalog)
An equivalent formulation uses the union operator
πsid(σcolour=0red0(Part)1Catalog ∪σcolour=0green0(Part)1Catalog).
The latter version can be refined by pushing the projection through the union:
πsid(σcolour=0red0(Part)1Catalog)∪πsid(σcolour=0green0(Part)1Catalog).
3. “Find the IDs of suppliers who supply some red part or are based at 21 George
Street.”
πsid(σcolour=0red0(Part)1Catalog)∪πsid(σaddress=021G.S.0(Supplier)).
4. “Find the names of suppliers who supply some red part or are based at 21 George
Street.”
πsname(σcolour=0red0(Part)1Catalog 1Supplier)
∪πsname(σaddress=021G.S.0(Supplier)).
Alternatively, we can pull the projection on sname to the top level.
πsname(σcolour=0red0(Part)1Catalog 1Supplier
∪σaddress=021G.S.0(Supplier)).
1
pf3
pf4

Partial preview of the text

Download Relational Algebra: Sample Solutions and more Study notes Algebra in PDF only on Docsity!

Introduction to Databases Fall-Winter 2009/

Exercises Michail Kazimianec Werner Nutt Damiano Somenzi

Relational Algebra: Sample Solutions

Note that the solutions given here are samples, i.e., there may be many more ways to express these queries in relational algebra.

  1. Write queries in relational algebra

Write the following queries in relational algebra.

  1. “Find the names of suppliers who supply some red part.”

π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.

  1. “Find the IDs of suppliers who supply some red or green part.”

π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).

  1. “Find the IDs of suppliers who supply some red part or are based at 21 George Street.”

πsid(σcolour=′red′ (Part) 1 Catalog) ∪ πsid(σaddress=′21G.S.′ (Supplier)).

  1. “Find the names of suppliers who supply some red part or are based at 21 George Street.”

π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)).

  1. “Find the IDs of suppliers who supply some red part and some green part.”

π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).

  1. “Find pairs of sids such that the supplier with the first sid charges more for some part than the supplier with the second sid.” First, by creating temporary copies we introduce two versions of 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.

  1. “Find the IDs of suppliers who supply only red parts.”

πsid(Supplier) \ πsid(Catalog 1 σcolour 6 =′red′^ (Part)).

  1. “Find the IDs of suppliers who supply every 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.)

  1. πsname(πsid,sname(σcolour=′red′^ (Part) 1 σcost< 100 (Catalog) 1 Supplier) ∩

π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.