Relational Algebra: Sample Solutions, Study notes of Algebra

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

Typology: Study notes

2021/2022

Uploaded on 09/07/2022

nabeel_kk
nabeel_kk šŸ‡øšŸ‡¦

4.6

(65)

1.3K documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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.