DB Review Session
description
Transcript of DB Review Session
DB Review Session
ER Diagrams 1
Where does the salary attribute belong?
Actorid
name
address
birthday
Acted In Movie
title
type
year
salary
id
name
Actor Acted In Movie
The entities in a relationship set identify the relationship
role
title
ER Diagrams 2
id
name
Actor Acted In Movie
The entities in a relationship set identify the relationship
How would we store information about a person who acted in one movie in several roles?
titleRole
title
ER Diagrams 2
ER Diagrams 3
Personid
name
age
FatherOf
Where would you put the arrow?
father
child
ER Diagrams 3
Personid
name
age
FatherOf
father
child
ER Diagrams 4
Directorid
name
produced Movie title
Actorid name
What does this mean?
A movie has at most one actor and one director
What does this diagram mean?
Directorid
name
Directed Movie title
A movie has exactly one director.
A director directs at least one movie.
ER Diagrams 5
Copy
copy number condition
Copy Of
Borrowed
Person
id
Booktitle
author
isbnOwned By
Libraryname
ER Diagrams 6
snopno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
P2
pno
sno
=
Division 1
snopno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
P2
pno
sno
=
Division 1
S1S2S3S4
snopno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
P2
P4
pno sno
=
Division 2
snopno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
P2
P4
pno sno
=
Division 2
S1S4
snopno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
P1
P2
P4
pno sno
=
Division 3
snopno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
pno sno
=
Division 3
S1P1
P2
P4
SQL Aggregation 1
SELECT B.bid, COUNT(*)
FROM Boats B, Reserves R
WHERE R.bid=B.bid and B.color=‘red’
GROUP BY B.bid
What does this query return?
Tuples:
(id of a reserved red boat, # of reservations of the red boat)
SELECT B.bid, COUNT(*)
FROM Boats B, Reserves R
WHERE R.bid=B.bid
GROUP BY B.bid, B.color
HAVING B.color=‘red’
SQL Aggregation 2
What if we put the condition into the HAVING clause?
We have also to put the color in the grouping list!
Aggregation 3
SELECT color
FROM Boats B
GROUP BY color
HAVING max(count(bid))
What is wrong with this?
How would you fix it?
The Color for which there are the most boats
SELECT color
FROM Boats B
GROUP BY color
HAVING count(bid) >= ALL
(SELECT count(bid)
FROM Boats
GROUP BY Color)
SQL Aggregation 3
The Color for which there are the most boats
Compute Closure(X, F)
C := X
while there is a V W in F such that (V C)and (W C) do
C := C Wreturn C
Attribute Closure 1
R=ABCDE F={ABC, CEB, DA, BCE}
Attribute Closure 2
{A}+ =
{A,B}+ =
{B,D}+ =
{A}
{A,B,C,E}
{A,B,C,D,E}
IsDependencyPreserving(F,R1…k)for each X->Y in F do
if not IsPreserved(X,Y,R1…k)return false
return true
Dependency Preservation 1a
/* check if X->Y is preserved */IsPreserved(X,Y,R1…k)Z:=Xwhile changes to Z occur do for i=1 to k do Z:= Z ((Z Ri)+ Ri) if YZ return trueelse return false
Dependency Preservation 1b
Is the following decomposition dependency preserving?
R = ABCDE F = {A -> ABCDE, BC -> A, DE -> C} R1 = ABDE, R2 = DEC
Dependency Preservation 2
No! BC->A is not preserved
R = ABC. For each F below, decide whether R is in BCNF/3NF:
Normal Forms
F = {} F = {A -> B} F = {A -> B, A -> C} F = {A -> B, B -> C} F = {A -> B, BC -> A}