You are on page 1of 13

CS 500: Fundamentals of Databases

Midterm exam solutions

Julia Stoyanovich (stoyanovich@drexel.edu)


ER modeling
Stars are identified by a name and are further described by
brightness and metallicity.

Planets are identified by a name and are further described by


diameter and mass. Each planet orbits exactly one star, and when
we record this we also record information about the orbital
period.

Satellites orbit planets, and are only included in our database if the
planet they orbit is in the database. A satellite has a name, but two
satellites that orbit two different planets may have the same name.

Deities are identified by a name and a mythology (e.g., Mars is a


deity of the ancient Roman mythology). Some planets are named
after deities. If a planet is named after a deity, then its just one
deity that gives the planet its name.

Julia Stoyanovich 2
ER modeling
name

Satellites

diameter
mass orbital_period
name

Planets orbit

named
after
period orbit

name mythology
name brightness

metallicity
Deities
Stars

Julia Stoyanovich 3
ER to relational translation
a1 a2

b1 b2 c1 c2

B rel C

create table B ( create table A_and_Rel (


b1 integer primary key, a1 integer primary key,
b2 integer a2 integer,
); b1 integer not null,
c1 integer not null,
create table C ( c2 integer not null,
c1 integer, unique (c1, c2),
c2 integer, foreign key (b1) references B(b1),
primary key (c1, c2) foreign key (c1,c2) references C(c1,c2)
); );

Julia Stoyanovich 4
ER to relational translation
r

a1 a2
rel1 b1 b2

A B
rel2
create table B (
b1 integer primary key,
b2 integer
);

create table A_and_Rel2 ( create table Rel1 (


a1 integer primary key, a1 integer,
a2 integer, b1 integer primary key,
b1 integer not null, r integer,
foreign key (b1) foreign key (a1) references A(a1),
references B(b1) foreign key (b1) references B(b1)
); );

Julia Stoyanovich 5
Relational algebra and SQL
Cocktails (cname, price) Recipes (cname, iname, units)
cname price cname iname units
Aperol spritz $10 Gimlet gin 4
Gimlet $14
Gimlet lemon juice 1
Manhattan $10
Gin fizz gin 3
Whiskey sour $12
Gin fizz $11 Gin fizz lemon juice 2

Gin fizz soda water 2


Ingredients (iname, unit_cost, ABV)
Manhattan whiskey 3
iname unit_cost ABV
Manhattan vermouth 1
gin $1 40
whiskey $2 43 Whiskey sour whiskey 2

vodka $1 40 Whiskey sour lemon juice 1

vermouth $0.5 18
lemon juice $0.1 0
soda water $0.1 0

Julia Stoyanovich 6
Relational algebra and SQL
Cocktails (cname, price)
Ingredients (iname, unit_cost, ABV)
Recipes (cname, iname, units)

(a) (Relational Algebra) List the names of ingredients that either cost less than $1
or are not used in any recipes in our database. The result should have the
schema (iname).

( iname ( cost<1 I )) ( iname I I .iname (I I .iname=R.iname R))


or

iname ( cost<$1 I ) ( iname I iname R)

Julia Stoyanovich 7
Relational algebra and SQL
Cocktails (cname, price)
Ingredients (iname, unit_cost, ABV)
Recipes (cname, iname, units)

(b) (SQL) List the names of ingredients that either cost less than $1 or are not
used in any recipes in our database. The result should have the schema
(iname).
select iname
from Ingredients
where unit_cost < 1
UNION
(select iname
from Ingredients
EXCEPT
select I.iname
from Ingredients I, Recipes R
where I.iname = R.iname );

Julia Stoyanovich 8
Relational algebra and SQL
Cocktails (cname, price)
Ingredients (iname, unit_cost, ABV)
Recipes (cname, iname, units)

(c) (SQL) List pairs of ingredients that are used in the same cocktail. Result
should have the schema (iname1, iname2, cname). Naturally, iname1 and
iname2 should refer to different ingredients. Do not list the same pair more than
once, i.e., you should not list both (gin, lemon juice, Gimlet) and (lemon juice,
gin, Gimlet).

select R1.iname, R2.iname, R1.cname


from Recipes R1, Recipes R2
where R1.cname = R2.cname
and R1.iname < R2.iname;

Julia Stoyanovich 9
Relational algebra and SQL
Cocktails (cname, price)
Ingredients (iname, unit_cost, ABV)
Recipes (cname, iname, units)

(d) (SQL) List names of cocktails together with the profit made on the sale of each
of them. For a cocktail, we define profit as the difference between its price and
the combined cost of their ingredients. Only include information about cocktails
for which we know the recipe. Order results by profit from higher to lower.
Result should have the schema (cname, profit).

select C.cname, C.price SUM (R.units * I.unit_cost) as profit


from Cocktails C, Recipes R, Ingredients I
where C.cname = R.cname
and R.iname = I.iname
group by C.cname, C.price
order by profit desc;

Julia Stoyanovich 10
Normalization
Consider relation R (ABCDE) with functional dependencies (FDs):

S = {A B ; A D ; B C ; BC D ; BE A}

(a) (5 points) What are the candidate keys of R?

The sets attributes that have all attributes of R in their closure are AE
and BE. These are the candidate keys of R under S.

(b) (5 points) Compute T, the minimum basis of the set of FDs in S.

T = {A B ; B C ; B D ; BE A}

Julia Stoyanovich 11
Normalization
Consider relation R (ABCDE) with functional dependencies (FDs):

S = {A B ; A D ; B C ; BC D ; BE A}

(c) (5 points) For each FD in T, state whether or not it violates BCNF and
3NF. Clearly explain your answer for each FD.

A B violates BCNF since A is not a candidate key or a superkey and the FD


is not trivial; this FD does not violate 3NF, since B is part of the
candidate key BE

BC violates both BCNF and 3NF, since B is not a candidate key or a


superkey, the FD is not trivial, and D is not part of any candidate key

B D violates both BCNF and 3NF, since B is not a candidate key or a


superkey, the FD is not trivial, and D is not part of any candidate key

BE A does not violate BCNF, and therefore also does not violate 3NF since
BE is a candidate key of R

Julia Stoyanovich 12
Normalization
Consider relation R (ABCDE) with functional dependencies (FDs):
S = {A B ; A D ; B C ; BC D ; BE A}

(d)(5 points) Compute a decomposition of R into 3NF. Clearly mark the


candidate key or keys of each relation in the result of the decomposition.

We create a relation for each FD in the minimum basis T, computed in


step (b): R1(AB) with key A, R2(BC) with key B, R3(BD) with key B,
R4 (ABE) with key BE.

Note that we can take a union of R2 and R3, since they share a key,
and have R23(BCD) in the decomposition instead, with key B.

Note also that the attributes of R1 are a proper subset of the attributes
in R4, and so we only need R4 in the final result. So, the result is:
R23(BCD) with key B and R4(ABE) with keys A and BE.

Julia Stoyanovich 13

You might also like