(123doc) Assignment2 Solution Database
(123doc) Assignment2 Solution Database
Assignment 2
Dirk Van Gucht & Mo
Relational Algebra
- We write r(ABC) for a relational schema with relation name r and
attributes (or fields) A, B, and C. The underlined attribute is the
primary key.
Basic Algebra
1. Given following relational schema, write expressions of relational
algebra to answer the following queries.
1
The sample data for relations of question 1. These data are not used
to calculate results of following questions.
Product:
model maker type
1001 A PC
3001 B Printer
2001 C laptop
PC:
model speed ram hd price
1001 2.66 1024 250 2114
1002 1.42 512 250 955
1003 3.20 2048 160 1049
Laptop:
model speed ram hd screen price
2001 2.00 1024 250 15 2114
2002 1.73 512 80 24 955
2003 1.83 2048 60 20 1049
Printer:
model color type price
3001 true ink-jet 99
3002 false laser 239
3003 true laser 899
2
(a) What PC models have a speed of at least 3.00?
πmodel(σmodel≥3.00(P C))
(b) Find the model numbers of all color laser printers.
πmodel(σtype=laser∧color=true(P rinter))
(c) Which manufacturers make laptops with a hard disk of at least
100GB?
πmaker (P roduct Da σhd≥100 (Laptop))
(d) Find the model number and price of all products (of any type)
made by manufacturer B.
πmodel,price (σmaker=B (P roduct)) Da
(πmodel,price(P C) ∪ πmodel,price(Laptop) ∪ πmodel,price(P rinter))
(e) Find those manufacturers that sell Laptops, but not PC’s.
πmaker(σtype=Laptop(P roduct)) − πmaker(σtype=P C (Product))
(f) Find those manufacturers that sell all models of PCs and lazer
Printers.
πmaker(Product/(πmodel(PC) ∪ πmodel(σtype=lazer(Printer))))
(g) Find those manufacturers whose laptops have all ram sizes that
manufacturer B’s laptops have.
(πmaker,ram (P roduct Da Laptop)) / (πram (σmaker=B (P roduct) Da
Laptop))
(h) Find those manufacturers of at least two different computers
(PC’s or laptops) with speeds of at least 2.80. ρ(TEMP
1, πmodel(σspeed≥2.80(PC)) ∪
πmodel (σspeed≥2.80 (Laptop)))
ρ(T EM P 2, P roduct Da T EM P
1)
πmaker(σT EMP 2.maker=T EMP 3.maker∧T EMP 2.model TEMP 3.model(T EMP 2×
ρ(TEMP 3, TEMP 2)))
(i) Find the manufacturers of PC’s with at least two different
speeds.
πR1.maker (σR1.speed R2.speed∧R1.maker=R2.maker ((ρ(R1, (P roduct Da
P C)) × ρ(R2, (P roduct Da P C)))))
(j) Find the manufacturers who sell exactly two different models of
PC.
πR1.maker (σR1.model R2.model∧R1.maker=R2.maker (ρ(R1, (P roduct Da
P C)) × ρ(R2, (P roduct Da P C)))) −
πR1.maker (σR1.modelƒ=R2.model∧R1.modelƒ=R3.model∧R2.modelƒ=R3.model
∧R1.maker=R2.maker∧R1.maker=R3.maker
3
(ρ(R1, (P roduct Da P C)) × ρ(R2, (P roduct Da
P C)) × ρ(R3, (P roduct Da P C))))
(k) Find those pairs of PC models that have both the same speed
and RAM. A pair should be listed only once; e.g., list (i, j) but
not (j, i).
Hint: The model numbers can be compared.
πPC1.model,PC2.model
(σPC1.speed=PC2.speed∧PC1.ram=PC2.ram∧PC1.model>PC2.model
(ρ(PC1, PC) × ρ(PC2, PC)))
(l) Find the manufacturer(s) of the computer (PC or laptop) with
the highest available speed.
Hint: the highest speed means that it is not smaller than any
other speeds. If you can find all the speeds which are
smaller than some speed, you can solve this problem.
ρ(Computer, (πmodel,speed(PC) ∪ πmodel,speed(Laptop)))
4
Constraints
(a) A PC with a processor speed less than 2.00 must not sell for
more than $500.
σspeed<2.00∧price>500(P C) = ∅
5
3. Express the following constraints in relational algebra. The
constraints are based on the following relations.
Classes(class, type, country, numGuns, bore, displacement)
Ships(shipname, class, launched)
Battles(battlename, date)
Outcomes(shipname, battlename, result)
6
Special algebra
(a) R ∪ S
(b) R Da S
(c) σC (R) × S, for some condition C.
(d) πL(R) − S, for some list of attributes L.