Classroom Tutorial 1

advertisement
Classroom Tutorial 1
Andreas Gustafsson
Exercises from the book, solutions from the classroom sessions, January
26 and 27, 2012. There are small differences in the notation of the grouping
operator between these notes and the book – all variations are valid on the
exam.
1
Relational algebra
2.4.1
Structure
Product(maker, model, type)
PC(model, speed, ram, hd, price)
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)
Questions
a) Find those manufacturers that sell printers, but not PC’s.
b) What PC models have a speed of at least 2.50?
c) Which manufacturers make laptops with a hard disk of at least 120GB?
d) Find the model number and price of all products (of any type) made by manufacturer
C.
e) Find the model number of all black-and-white laser printers.
f) Find those hard-disk sizes that accur in two or more PC’s.
g) 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).
h) Find those manufacturers of at least two different computers (PC’s or laptops) with
speeds of at least 2.20.
i) Find the manufacturers who sell exactly three different models of PC.
j) Find the manufacturer(s) of the computer (PC or laptop) with the highest available
speed.
k) Find the manufacturers of PC’s with at least three different speeds.
1
Classroom Tutorial 1
Solutions
a) Πmaker (σtype=0 printer0 (P roduct)) − Πmaker (σtype=0 P C 0 (P roduct ./ P C))
b) Πmodel (σspeed>2.50 (P C))
c) Πmaker (σhd>120 (P roduct ./ Laptop))
d) Πmodel,price (σmaker=0 C 0 (P roduct ./ P C)∪σmaker=0 C 0 (P roduct ./ Laptop)∪σmaker=0 C 0 (P roduct ./
P rinter))
e) Πmodel (σtype=0 laser0 ∧¬color (P rinter)
f) Πhd (σcount≥2 (hd Gcount(∗) (P C)))
g) ΠP C1.model,P C2.model ρP C1 (P C) ./P C1.speed=P C2.speed∧P C1.ram=P C2.ram∧P C1.model<P C2.model ρP C2 (P C)
h) Πmaker (σcount≥2 (maker Gcount(∗) (P roduct ./ (Πmodel (σspeed≥2.20 (P C))∪Πmodel (σspeed≥2.20 (Laptop))))
i) Πmaker (σcount=3 (maker Gcount(model) (P roduct)))
j) Πmaker ((P roduct ./ P C)∪(P roduct ./ Laptop) ./ ρF oo(speed) (Gmax(speed) (Laptop∪P C))
k) Πmaker (σcount≥3 (maker Gcount(speed) (P roduct ./ P C)))
2.4.7
Suppose relations R and S have n tuples and m tuples respectively. Give the minimum
and maximum numbers of tuples that the results of the following expressions can have.
a) R ∪ S.
b) R ./ S.
c) σC (R) × S, for some condition C.
d) πL (R) − S, for some list of attributes L.
Solutions
a) R ∪ S ≡ (R ∪ S) − (R ∩ S). R ∩ S = ∅ ⇒ n + m The minimum is max(m, n) if one set
is a subset of the other.
b) All elements of the same type must match for a tuple to be included. To produce as
many tuples as possible by a natural join, we assume that one column in S are identical
to a column in R. This results in a cartesian product between tables. Maximum = n×m.
If no elements at all match, we dont get any tuples. Minimum = 0;
c) If C includes all tuples of R, we have the same case as in the previous case. If it
includes none, we get zero tuples in our result.
d) L does not restrict tuples in any way. Therefore the maximum is |R| (if the intersection
is empty), and the minimum is 0.
2
2 SQL
2
Classroom Tutorial 1
SQL
Structure
Movies(title, year, length, genre, studioName, producerC#)
StarsIn(movieTitle, movieYear, starName)
MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth)
Studio(name, address, presC#)
6.1.2
Questions
a) Find Aishwara Rai’s birthdate.
b) Find the address of Film City.
c) Find all the stars that appeared either in a movie made in 2000 or a movie with ”Story”
in the title.
d) Find all the stars who either are female or live in Mumbai (have string ”Mumbai” as a
part of their address).
e) Find all executives worth at least $20,000,000.
Solutions
a) select birthDate from MovieStar where name=’Aishwara Rai’;
b) select address from Studio where name=’Film City’;
c) select starName from StarsIn natural join Movies where movieYear=2000 or title like
’%Story%’;
d) select name from MovieStar where gender=’female’ or address like ’%Mumbai%’;
e) select name from MovieExec where netWorth>=20000000;
6.2.1
Questions
a) Who is the president of Film City?
b) Who were the male stars in ”Monsoon Wedding”?
c) Which stars appeared in movies produced by Sony in 2005?
d) Which executives are worth more than Subhash Ghai?
e) Which movies are longer than ”Pride and Prejudice”? (In the book it says ”Bride and
Prejudice”. I assume that is a typo).
3
Classroom Tutorial 1
Solutions
a) select name from MovieExec join Studio on presC#=cert# where name=”Film City”;
b) select name from MovieStar join StarsIn on starName=name where movieTitle=’Monsoon
Wedding’ and gender=’male’;
c) select starName from StarsIn join Movies on title=movieTitle and year=movieYear
where year=2005 and studioName=’Sony’;
d) select name from MovieExec where netWorth>(select netWorth from MovieExec where
name=’Subhash Ghai’);
e) select title from Movies where length>(select length from Movies where title=*Pride
and Prejudice’;
6.2.4
Questions
A general form of relational-algebra query is
π σC (R1 ./ R2 ./ · · · ./ Rk )
Here. L is an arbitrary list of attributes, and C is and arbitrary contition. The list of
relations R1 , R2 , · · · , Rk may include the same relation repeated several times, in which
case appropriate renamig may be assumed applied to the Ri ’s. Show how to express any
query of this form in SQL.
Solutions
select * from R1 , R2 , · · · , Rk where C;
4
Download