Professional Documents
Culture Documents
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.
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
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
);
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
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).
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).
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).
Julia Stoyanovich 10
Normalization
Consider relation R (ABCDE) with functional dependencies (FDs):
S = {A B ; A D ; B C ; BC D ; BE A}
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.
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.
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}
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