midterm exam solutions - drexel ccijulia/cs500/documents/lectures/midterm... · midterm exam...

13

Click here to load reader

Upload: trinhdieu

Post on 28-Aug-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

CS 500: Fundamentals of Databases

Midterm exam solutions

Julia Stoyanovich ([email protected])

Page 2: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

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 it’s just one deity that gives the planet its name.

2

Page 3: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

ER modeling

3

Deities

Planets

Satellites

orbit

name

diameter

name mythology

Stars

name brightness

metallicity

mass

name

orbitperiod

named after

orbital_period

Page 4: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

ER to relational translation

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

create table C ( c1 integer, c2 integer, primary key (c1, c2));

4

A

B Crel

b1 b2

a1 a2

c1 c2

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

Page 5: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

ER to relational translation

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

create table A_and_Rel2 ( a1 integer primary key, a2 integer, b1 integer not null, foreign key (b1) references B(b1));

5

create table Rel1 ( a1 integer, b1 integer primary key, r integer, foreign key (a1) references A(a1), foreign key (b1) references B(b1));

A B

rel2

b1 b2a1 a2 rel1

r

Page 6: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Relational algebra and SQL

6

iname unit_cost ABVgin $1 40

whiskey $2 43

vodka $1 40

vermouth $0.5 18

lemon juice $0.1 0

soda water $0.1 0

Cocktails (cname, price)

cname iname unitsGimlet gin 4

Gimlet lemon juice 1

Gin fizz gin 3

Gin fizz lemon juice 2

Gin fizz soda water 2

Manhattan whiskey 3

Manhattan vermouth 1

Whiskey sour whiskey 2

Whiskey sour lemon juice 1

Ingredients (iname, unit_cost, ABV)

cname priceAperol spritz $10

Gimlet $14

Manhattan $10

Whiskey sour $12

Gin fizz $11

Recipes (cname, iname, units)

Page 7: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Relational algebra and SQL

7

(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).

Cocktails (cname, price) Ingredients (iname, unit_cost, ABV)Recipes (cname, iname, units)

(π iname (σ cost<1I ))∪ (π iname I −π I .iname(I ▹◃ I .iname=R.iname R))

π iname(σ cost<$1I )∪ (π inameI −π inameR)

or

Page 8: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Relational algebra and SQL

8

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 inamefrom Ingredientswhere unit_cost < 1UNION(select iname from Ingredients EXCEPT select I.iname from Ingredients I, Recipes R where I.iname = R.iname );

Page 9: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Relational algebra and SQL

9

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.cnamefrom Recipes R1, Recipes R2where R1.cname = R2.cnameand R1.iname < R2.iname;

Page 10: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Relational algebra and SQL

10

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 profitfrom Cocktails C, Recipes R, Ingredients Iwhere C.cname = R.cnameand R.iname = I.inamegroup by C.cname, C.priceorder by profit desc;

Page 11: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Normalization

11

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}

Page 12: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Normalization

12

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.

• 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

• 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

• 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

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

A→ B

B→C

B→ D

BE→ A

Page 13: Midterm exam solutions - Drexel CCIjulia/cs500/documents/lectures/midterm... · Midterm exam solutions ... Relational algebra and SQL 6 iname unit_cost ABV gin $1 40 whiskey $2 43

Julia Stoyanovich

Normalization

13

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.