CZ2007 Introduction to
Database Systems (Week 5)
Topic 4: Third Normal Form (2)
1
This Lecture
n 3NF Decomposition
n Properties of 3NF
2
Exercise
n S = {AàC, ACàD, ADàB}
1. Transform the FDs to ensure that the right
hand side of each FD has only one attribute
2. See if any FD can be derived from the other
FDs. Remove those FDs one by one
3. Check if we can remove any attribute from
the left hand side of any FD
3
Exercise
n S = {AàC, ACàD, ADàB}
1. Transform the FDs to ensure that the right
hand side of each FD has only one attribute
n Results: M = {AàC, ACàD, ADàB}
2. See if any FD can be derived from the other
FDs. Remove those FDs one by one
4
Exercise
n M = {AàC, ACàD, ADàB}
2. See if any FD can be derived from the other FDs.
Remove those FDs one by one.
n Try AàC first
If AàC is removed, then the ones left would be
ACàD, ADàB
q With the remaining FDs, we find {A}+ = {A}
+
q Since {A} does not contain C, we know that AàC
cannot be derived from the remaining FDs
q Therefore, AàC is not redundant; cannot be removed
q
5
Exercise
n M = {AàC, ACàD, ADàB}
2. See if any FD can be derived from the other FDs.
Remove those FDs one by one.
n Next, try ACàD
If ACàD is removed, then the ones left would be
AàC, ADàB
q With the remaining FDs, we have {AC}+ = {AC}
+
q Since {AC} does not contain D, we know that ACàD
cannot be derived from the remaining FDs
q Therefore, ACàD cannot be removed
q
6
Exercise
n M = {AàC, ACàD, ADàB}
2. See if any FD can be derived from the other FDs.
Remove those FDs one by one.
n Next, try ADàB
If ADàB is removed, then the ones left would be
AàC, ACàD
q With the remaining FDs, we have {AD}+ = {ADC}
+
q Since {AD} does not contain B, we know that ADàB
cannot be derived from the remaining FDs
q Therefore, ADàB cannot be removed
q
7
Exercise
M = {AàC, ACàD, ADàB}
3. Check if we can remove any attribute from the left
hand side of any FD
n First, try to remove A from ACàD
n
It results in CàD
q Can CàD be derived from M?
q {C}+ = {C} given M.
q Since {C}+ does not contain D, we know that CàD cannot
be derived from M and
q we should not arbitrarily introduce new FD to M
q Therefore, A should remain in ACàD
q
8
Exercise
n M = {AàC, ACàD, ADàB}
3. Check if we can remove any attribute from the
left hand side of any FD
n Next, try to remove C from ACàD
It results in AàD
q Can AàD be derived from M?
q {A}+ = {ABCD} given M.
q Since {A}+ contains D, we know that AàD can be
derived from M
q Therefore, C can be removed from ACàD
q
n New M = {AàC, AàD, ADàB}
9
Exercise
n New M = {AàC, AàD, ADàB}
3. Check if we can remove any attribute from the
left hand side of any FD
n Next, try to remove A from ADàB
It results in DàB
q Can DàB be derived from M?
q {D}+ = {D} given M.
q Since {D}+ does not contain B, we know that DàB
cannot be derived from M
q Therefore, A should remain in ADàB
q
10
Exercise
n M = {AàC, AàD, ADàB}
3. Check if we can remove any attribute from the
left hand side of any FD
n Next, try to remove D from ADàB
It results in AàB
q Can AàB be derived from M?
q {A}+ = {ABCD} given M.
q Since {A}+ contains B, we know that AàB can be
derived from M
q Therefore, D can be removed from ADàB
q
n New M = {AàC, AàD, AàB}; done
11
3NF Decomposition Algorithm
n Given:
Table R(A, B, C, D)
q A minimal basis {AàB, AàC, CàD}
q
n Step 1: Combine those FDs with the same left
hand side
q
Result: {AàBC, CàD}
n Step 2: For each FD, create a table that contains
all attributes in the FD
q
Result: R1(A, B, C), R2(C, D)
n Step 3: Remove redundant tables (if any)
n Tricky issue: Sometimes we also need to add an
additional table (next slide)
12
3NF Decomposition Algorithm
n
Given:
Table R(A, B, C, D)
q A minimal basis {AàB, CàD}
q
n
Step 1: Combine those FDs with the same left hand side
q
n
Result: {AàB, CàD}
Step 2: For each FD, create a table that contains all attributes
in the FD
q
Result: R1(A, B), R2(C, D)
Step 3: Remove redundant tables (if any)
n Problem: R1 and R2 do not ensure lossless join
n Solution: Add a table that contains a key of the original table R
n Key of R: {AC}
n Additional table to add: R3(A, C)
n Final result: R1(A, B), R2(C, D), R3(A, C)
n
13
3NF Decomposition Algorithm
n
Given:
Table R(A, B, C, D)
q A minimal basis {AàB, CàD}
q
n
Step 1: Combine those FDs with the same left hand side
q
n
Step 2: For each FD, create a table that contains all
attributes in the FD
q
n
Result: R1(A, B), R2(C, D)
Step 3: If no table contain a key of the original table, add
a table that contains a key of the original table
q
n
Result: {AàB, CàD}
Result: R1(A, B), R2(C, D), R3(A, C)
Step 4: Remove redundant tables (if any)
14
This Lecture
n 3NF Decomposition
n Properties of 3NF
15
Minimal Basis is not always unique
n For given set of FDs, its minimal basis may not
be unique
n Example:
Given R(A, B, C) and
{AàB, AàC, BàC, BàA, CàA, CàB}
q Minimal basis 1: {AàB, BàC, CàA}
q Minimal basis 2: {AàC, BàC, CàA, CàB}
q
n Different minimal basis may lead to different
3NF decompositions
16
BCNF vs. 3NF
n
BCNF: For any non-trivial FD
q
n
its left hand side (lhs) is a superkey
3NF: For any non-trivial FD
Either its lhs is a superkey
q Or each attribute on its right hand side either appear in the
lhs or in a key
q
Observation: BCNF is stricter than 3NF
n Therefore
n
A table that satisfies BCNF must satisfy 3NF, but not vice
versa
q A table that violates 3NF must violate BCNF, but not vice
versa
q
17
BCNF vs. 3NF
n
BCNF Decomposition:
Avoids insertion, deletion, and update anomalies
q Eliminates most redundancies
q But does not always preserve all FDs
q
n
3NF Decomposition:
Avoids insertion, deletion, and update anomalies
q May lead to a bit more redundancy than BCNF
q Always preserve all FDs
q
So which one to use?
n A logical approach
n
Go for a BCNF decomposition first
q If it preserves all FDs, then we are done
q If not, then go for a 3NF decomposition instead
q
18
Why Does 3NF Preserve All FDs?
Given: A table R, and a set S of FDs
n Step 1: Derive a minimal basis of S
n Step 2: In the minimal basis, combine the FDs whose
left hand sides are the same
n Step 3: Create a table for each FD remained
n Step 4: If none of the tables contain a key of the
original table R, create a table that contains a key of R
n Step 5: Remove redundant tables
n Rationale: Because of Step 3 (minimal basis preserves
FDs; no redundant FDs)
n
19
Next lecture:
Topic 5: Relational Algebra (1)
20