Uploaded by egyahmed.ezzat120

Relational Algebra Sheet

advertisement
CSCI03C02
Database Systems
Relational Algebra Exercises
1. Consider the relations Student, Course, and StudentCourse as follows:
2. Consider the following relational database schema. It is intended to represent the holdings of a multibranch library. A sample database instance is also given.
Branch
Titles
Holdings
BCode Librarian
B1
B2
B3
Title
BCode Librarian
Title Author Publisher
Branch Title #copies
Addre ss
John Sm ith
2 Angle sea Rd
34 P ea rse St
Ma ry Jones
Fra ncis Owens Grange X
Author
Add ress
P ublishe r
Ma cm illa n
Susannah
Ann Brown
Stop P ress
How to Fish
Am y Fly
W iley
A History of Dublin David Little
Com puters
Bla ise P asc al Apple woods
Ma cm illa n
The W ife
Ann Brown
Branc h Title
B1
B1
B1
B2
B2
B2
B3
B3
B3
B3
#copies
Susannah
How to
A hist
How to
Com puters
The W ife
A hist ..
Com puters
Susannah
The W ife
3
2
1
4
2
3
1
4
3
1
(a) Give a series of relational algebra operations (select, project, join, difference, division, ...) to
produce the following four relations:
(i)
(Librarians) is a list of the names of the branch librarians.
(ii)
(Brown_Branches) is the set of branches which have holdings of books by Ann Brown.
(iii)
(No_Browns) is the set of branches which have no holdings of any books by Ann Brown.
(Use the Brown_Branches relation from (ii).
(b) To retrieve each of the following, write a SQL statement and translate it to an equivalent algebra
expression:
(i)
the names of all library books published by Macmillan.
(ii)
branches that hold any books by Ann Brown (using a nested subquery).
(iii)
branches that hold any books by Ann Brown (without using a nested subquery).
(iv)
the total number of books held at each branch.
3. Specify relational algebra expressions for the following on the parts-supplier-shipments database
with the following schema.
SP S#
S1
S S# SNAME STATUS CITY
S1
20
London S1
S1 Smith
10
Paris
S2 Jones
S2
30
Paris
S3 Blake
S2
S3
P P#
P1
P2
P3
P4
PNAME
Nut
Bolt
Screw
Screw
COLOUR WEIGHT CITY
Red
12
London
Green
17
Paris
Blue
17
Rome
Red
14
London
(a) Names of suppliers who supply part P2.
(b) Names of suppliers who supply at least one red part.
(c) Names of suppliers who supply all parts.
(d) Names of suppliers who do not supply part P2.
4. Consider the following schema:
Suppliers(sid, sname : string, address : string)
Parts(pid, pname : string, colour : string)
Catalog(sid, pid, cost : real)
P#
P1
P2
P3
P1
P2
P2
QTY
300
200
400
300
400
200
The relation Suppliers stores supplies and the key of that relation is sid. The relation Parts stores parts,
and pid is the key of that relation. Finally, Catalog stores which supplier supplies which part at which
cost. The key is the combination of the two attributes sid and pid
For each of the following relational algebra queries, say what they mean and translate them into SQL.
5. Consider the following relational DB :
SUPERMARKET ( supermarket-id, manager-id, phone, city )
MANAGER ( manager-id, name, salary, address )
ITEM ( item-code, description, item-price )
ITEMSTOCK ( supermarket-id, item-code, quantity-in-stock )
Primary keys are underlined while foreign keys are in italic.
It is required to write the necessary OPTIMIZED relational Algebra to:
(i) Retrieve all the information of items in supermarket "S101".
(ii) For each manager, retrieve manager-id, name, supermarket-id which he manages and phone.
(iii) Retrieve all items information including the supermarket in which it exists where item-price is
more than LE 100.
Download