lecture*17* 10/07/15 - penn state college of...
TRANSCRIPT
CMPSC431W: Database Management Systems
Instructor: Yu-‐San Lin [email protected]
Course Website: hCp://www.cse.psu.edu/~yul189/cmpsc431w
Slides based on McGraw-‐Hill & Dr. Wang-‐Chien Lee
Lecture 17 10/07/15
1
1NF, 2NF & 3NF
• 1NF (remove non-‐atomic values) – Test: should not have non-‐atomic aCributes – Remedy: create relaWons for each non-‐atomic aCribute
• 2NF (remove parWal dependency) – Test: no non-‐key aCributes be funcWonally dependent on a part of the primary key
– Remedy: decompose and create a new relaWon for each parWal dependency
2
1NF, 2NF & 3NF (cont.)
• 3NF (remove transiWve dependency) – Test: should not have a non-‐key aCribute funcWonally determined by another (or a set of) non-‐key aCributes
– Remedy: decompose and create a relaWon that includes the non-‐key aCributes that funcWonally determines other non-‐key aCributes
3
Boyce-‐Codd Normal Form (BCNF)
• RelaWon R with FDs F is in BCNF if, for all X à A in F+ – A ∈ X (called a trivial FD), OR – X contains a key for R (i.e., X is a superkey)
• IntuiWvely: the only nontrivial dependencies are those in which a _____ determines some aCribute(s) – Each tuple can be seen as an enWty or relaWonship, idenWfied by a key and described by aCributes
4
Boyce-‐Codd Normal Form (BCNF)
• If we use ovals to denote aCributes or sets of aCributes and draw arcs to indicate FDs, a relaWon in BCNF has the structure:
5
Key Non-‐key aCr 1
Non-‐key aCr 2
Non-‐key aCr k
…
Boyce-‐Codd Normal Form (BCNF)
• BCNF ensures that no redundancy at all can be detected using FD
• Example:
– Given X à A, we know the value for ? is _____ – Then, is this instance in BCNF?
6
X Y A
x y1 a
x y2 ?
ProperWes of DecomposiWons
• It is important to check that a decomposiWons does not introduce new problems
• We should check whether a decomposiWon – allows us to _______ the original relaWon – allows us to check _________________efficiently
• Two types of decomposiWon: – ___________________ decomposiWon – ___________________ decomposiWon
7
Lossless-‐Join DecomposiWon
Defini1on: A decomposiWon of R into two schemas with aCribute sets X and Y is said to be a lossless-‐join decomposiWon with respect to F if, for every instance r of R that saWsfies the dependencies in F,
i.e., we can recover the original relaWon from the decomposed relaWons
8
Lossless-‐Join DecomposiWon (cont.)
• The definiWon can easily be extended to cover a decomposiWon of R into more than two relaWons, since
* Note that the other direcWon does not hold
9
Is this decomposiWon lossless-‐join?
10
S P D
s1 p1 d1
s2 p2 d2
s3 p1 d3
Instance r
S P
s1 p1
s2 p2
s3 p1
P D
p1 d1
p2 d2
p1 d3
πSP(r) πPD(r)
πSP(r) ⋈ πPD(r)
Revert back
S P D
s1 p1 d1
s2 p2 d2
s3 p1 d3
s1 p1 d3
s3 p1 d1
Decompose into
We call such decomposiWon _____
Theorem 3
Let R be a rela*on and F be a set of FDs that hold over R. The decomposi*on of R into rela*ons with a;ribute sets R1 and R2 is lossless is and only if F+ contains either: • _______________, OR • _______________
11
Use this simple test to check whether a decomposiWon is lossless
Is this decomposiWon lossless-‐join?
• Use Theorem 3 to check:
12
ssn name lot ra1ng hourly_wages hours_worked
123-‐22-‐3666 Aoshoo 48 8 10 40
231-‐31-‐5368 Smiley 22 8 10 30
131-‐24-‐3650 Smethurst 35 5 7 30
434-‐26-‐3751 Guldu 35 5 7 32
612-‐67-‐4134 Madayan 35 8 10 40
ssn name lot ra1ng hours_worked
123-‐22-‐3666 Aoshoo 48 8 40
231-‐31-‐5368 Smiley 22 8 30
131-‐24-‐3650 Smethurst 35 5 30
434-‐26-‐3751 Guldu 35 5 32
612-‐67-‐4134 Madayan 35 8 40
ra1ng hourly_wages
8 10
5 7
Lossless-‐Join’s ObservaWon #1
If an FD X ! Y holds over a rela*on R and X ∩ Y is empty, the decomposi*on of R into R – Y and XY is lossless. * X appears in both R – Y (since X ∩ Y is empty) and XY, and it is a ____ for XY.
13
Lossless-‐Join’s ObservaWon #2
14
R Lossless-‐join decompose
R1
R2
Lossless-‐join decompose
R11
R12
Dependency-‐Preserving DecomposiWon
• Is this in BCNF?
Contracts (contracWd, supplierid, projecWd, depWd, parWd, qty, value) Schema: CSJDPQV Given FDs: 1. C à CSJDPQV 2. JP à C 3. SD à P
15
Dependency-‐Preserving DecomposiWon (cont.)
However: Enforcing JP à C into the decomposed relaWons requires an expensive join of the two relaWons whenever a tuple is inserted. è Such decomposiWon ______ dependency-‐preserving
16
CSJDPQV
Lossless-‐join decompose CSJDQV
SDP
Dependency-‐Preserving DecomposiWon (cont.)
• Let – R: a relaWon schema that is decomposed into two schemas with aCribute sets X and Y
– F: a set of FDs over R • ProjecWon of F on X: the set of FDs in F+ that involve only aCributes in X
• Note: U à V in F+ is in FX only if all the aCributes in Y and V are in X
17
Dependency-‐Preserving DecomposiWon (cont.)
Defini1on: the decomposiWon of relaWon schema R with FDs F into schemas with aCribute sets X and Y is dependency-‐preserving if
i.e., if we take the dependencies in FX and FY and compute the closure of their union, we get back all dependencies in F+
18