0% found this document useful (0 votes)
65 views

Lect11 BCNF

The document discusses normal forms for database tables. It begins by introducing functional dependencies and superkeys. It then discusses Boyce-Codd normal form (BCNF), which requires that every non-trivial functional dependency is dependent on a superkey attribute. The document provides an example to illustrate BCNF and explains that BCNF prevents redundancy from attributes dependent on non-superkey attributes. It concludes by outlining the algorithm to check if a table satisfies BCNF using closures to derive keys and functional dependencies.

Uploaded by

Sourabh Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Lect11 BCNF

The document discusses normal forms for database tables. It begins by introducing functional dependencies and superkeys. It then discusses Boyce-Codd normal form (BCNF), which requires that every non-trivial functional dependency is dependent on a superkey attribute. The document provides an example to illustrate BCNF and explains that BCNF prevents redundancy from attributes dependent on non-superkey attributes. It concludes by outlining the algorithm to check if a table satisfies BCNF using closures to derive keys and functional dependencies.

Uploaded by

Sourabh Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

CS2102

Database Systems

1
Last Lecture R A B C
AB, BC
 Functional dependencies (FD)
 Example above: AB, BC
 Superkeys of a table R
 A set of attribute that can decide all other attributes in R
 Example above: A, AB, AC, ABC are all superkeys of R
 Keys
 A superkey that is minimal
 Example above: A is the only key of R
 Finding superkeys/keys from R
 Using closures, based on the given FDs

2
Coming Next NF

 Normal forms
You

3
Coming Next NF

 Normal forms
You

1NF 2NF 3NF BCNF 4NF



4
Normal Forms
 Conditions that a “good” table should satisfy
 Various normal forms 
(in increasing order of strictness)
 1st NF Easy to satisfy
 2nd NF May have high redundancy

 3rd NF (3NF)
 Boyce‐Codd NF (BCNF)
 4th NF
Very little redundancy
 5th NF Not always possible to satisfy
 6th NF

5
Normal Forms
 Conditions that a “good” table should satisfy
 Various normal forms 
(in increasing order of strictness)
 1st NF
 2nd NF
 3rd NF (3NF) Get rid of most redundancies
 Boyce‐Codd NF (BCNF) Always possible to satisfy

 4th NF
 5th NF
 6th NF

6
Roadmap

 We will focus on 3NF and BCNF since they are the 
most commonly used NF
 We will start from BCNF since it is conceptually 
simpler

7
Non‐trivial and Decomposed FD

 To simplify our discussions of BCNF and 3NF, we 
will focus on non‐trivial and decomposed FDs
 Decomposed FD: an FD whose right hand side has 
only one attribute
 E.g., AC, BCD, DEFE
 Note: a non‐decomposed FD can always be 
transformed into an equivalent set of 
decomposed FDs
 E.g., BCDE   <==>   BCD and BCE

8
Non‐trivial and Decomposed FD

 To simplify our discussions of BCNF and 3NF, we 
will focus on non‐trivial and decomposed FDs
 Non‐trivial and decomposed FD: a decomposed 
FD whose right hand side does not appear on the 
left hand side
 E.g., AC, BCD
 We will check normal forms based on the non‐
trivial and decomposed FDs on a table
 How do we derive such FDs?
9
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 1: Consider all attribute sets in R

 {A} {B} {C}


 {AB} {AC} {BC}

10
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 2: Compute the closure of each subset

 {A} {B} {C}


 {AB} {AC} {BC}}
C}

11
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 2: Compute the closure of each subset

 {A}+ = {ABC}, {B}+ = {ABC}, {C}+ = {C}


 {AB}+ = {ABC}, {AC}+ = {ABC}, {BC}+ = {ABC}
C}

12
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 2: Compute the closure of each subset

 {A}+ = {ABC},  {B}+ = {ABC}, {C}+ = {C}


 {AB}+ = {ABC}, {AC}+ = {ABC}, {BC}+ = {ABC}

13
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 3: From each closure, remove the "trivial" 
attributes
 {A}+ = {ABC},  {B}+ = {ABC}, {C}+ = {C}
 {AB}+ = {ABC}, {AC}+ = {ABC}, {BC}+ = {ABC}

14
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 3: From each closure, remove the "trivial" 
attributes
 {A}+ = {ABC},  {B}+ = {ABC}, {C}+ = {C}
 {AB}+ = {ABC}, {AC}+ = {ABC}, {BC}+ = {ABC}

15
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 4: Derive non‐trivial and decomposed FDs 
from each closure
 {A}+ = {ABC},  {B}+ = {ABC}, {C}+ = {C}
 {AB}+ = {ABC}, {AC}+ = {ABC}, {BC}+ = {ABC}

16
Non‐trivial and Decomposed FD

 Non‐trivial and decomposed FDs can be found 
using closures, in a way similar to finding keys
 Example: R(A, B, C), with AB, BA, BC given
 Step 4: Derive non‐trivial and decomposed FDs 
from each closure
 AB,  AC, BA, BC
 ABC, ACB, BCA

17
BCNF: Definition
 A table R is in BCNF, if every non‐trivial and 
decomposed FD has a superkey as its left hand side
 Example: R(A, B, C), with AB, BA, BC given
 Key: A, B
 Non‐trivial and decomposed FDs on R: 
 AB,  AC, BA, BC
 ABC, ACB, BCA
 For each of the above FD, the left hand side is a 
superkey
 So R satisfies BCNF  
18
BCNF: Definition
 A table R is in BCNF, if every non‐trivial and 
decomposed FD has a superkey as its left hand 
side
 Example: R(A, B, C), with AB, BC given
 Key: A
 Non‐trivial and decomposed FDs on R: 
 AB,  AC, BC
 ABC, ACB
 The left hand side of BC is not a superkey
 So R does not satisfy BCNF  

19
BCNF: Intuition

 BCNF requires that if there is a non‐trivial and 
decomposed FD A1A2…AnB, then A1A2…An must 
be a superkey
 In other words, any attribute B can depend only
on superkeys
 "In superman superkeys we trust!"
 Any dependency on non‐superkeys
is prohibited by BCNF

20
BCNF: Intuition
 In other words, any attribute B can depend only on 
superkeys
 Why does this make sense?
 Suppose that B depends on a non‐superkey C1C2…Cn
 Since C1C2…Cn is not a superkey, the same C1C2…Cn
may appear multiple times in the table
 Whenever this happens, the same B would appear 
multiple times in the table
 This leads to redundancy
 BCNF prevents this from happening
21
BCNF: Intuition
Name NRIC Phone Address
Alice 1234 67899876 Jurong East
Alice 1234 83848384 Jurong East
Bob 5678 98765432 Pasir Ris
 Key: {NRIC, Phone}
 NRIC  Name, Address, which violates BCNF
 Since NRIC is not a superkey, the same NRIC can appear 
multiple times in the table
 Every time the same NRIC is repeated, the corresponding 
Name and Address are also be repeated
 This leads to redundancy
 BCNF prevents this
22
Coming Next

 How do we check whether a table is in BCNF?

23
BCNF Check
 A table R is in BCNF, if every non‐trivial and 
decomposed FD has a superkey as its left hand 
side
 Algorithm for checking BCNF
 Compute the closure of each attribute subset
 Derive the keys of R (using closures)
 Derive all non‐trivial and decomposed FDs on R (again, 
using closures)
 Check the non‐trivial and decomposed FDs to see if 
they satisfy the BCNF requirement
 If all of them satisfy the requirement, then R is in BCNF

24
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA

25
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


1. Compute the closure for each subset of the attributes 
in R
+ + + +
 {A} = {A},  {B} = {B},  {C} = {ACD},  {D} = {AD}
+ + +
 {AB} = {ABCD},  {AC} = {ACD},  {AD} = {AD}
+ + +
 {BC} = {ABCD},  {BD} = {ABCD},  {CD} = {ACD}
+ + +
 {ABC} = {ABD} = {BCD} = {ABCD} 
+
 {ACD} = {ACD}
+
 {ABCD} = {ABCD}

26
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R

 {A}+= {A},  {B}+= {B},  {C}+= {ACD},  {D}+= {AD}
 {AB}+= {ABCD},  {AC}+= {ACD},  {AD}+= {AD}
 {BC}+= {ABCD},  {BD}+= {ABCD},  {CD}+= {ACD}
 {ABC}+= {ABD}+= {BCD}+ = {ABCD} 
 {ACD}+ = {ACD}
 {ABCD}+ = {ABCD}

27
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD

 {A}+= {A},  {B}+= {B},  {C}+= {ACD},  {D}+= {AD}
 {AB}+= {ABCD},  {AC}+= {ACD},  {AD}+= {AD}
 {BC}+= {ABCD},  {BD}+= {ABCD},  {CD}+= {ACD}
 {ABC}+= {ABD}+= {BCD}+ = {ABCD} 
 {ACD}+ = {ACD}
 {ABCD}+ = {ABCD}

28
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
+ + + +
 {A} = {A},  {B} = {B},  {C} = {ACD},  {D} = {AD}
+ + +
 {AB} = {ABCD},  {AC} = {ACD},  {AD} = {AD}
+ + +
 {BC} = {ABCD},  {BD} = {ABCD},  {CD} = {ACD}
+ + +
 {ABC} = {ABD} = {BCD} = {ABCD} 
+
 {ACD} = {ACD}
+
 {ABCD} = {ABCD}

29
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
 CA, CD, DA
+ + +
 {AB} = {ABCD},  {AC} = {ACD},  {AD} = {AD}
+ + +
 {BC} = {ABCD},  {BD} = {ABCD},  {CD} = {ACD}
+ + +
 {ABC} = {ABD} = {BCD} = {ABCD} 
+
 {ACD} = {ACD}
+
 {ABCD} = {ABCD}

30
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
 CA, CD, DA

 ABC, ABD, ACD
+ + +
 {BC} = {ABCD},  {BD} = {ABCD},  {CD} = {ACD}
+ + +
 {ABC} = {ABD} = {BCD} = {ABCD} 
+
 {ACD} = {ACD}
+
 {ABCD} = {ABCD}

31
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
 CA, CD, DA

 ABC, ABD, ACD

 BCA, BCD, BDA, BDC,  CDA
+ + +
 {ABC} = {ABD} = {BCD} = {ABCD} 
+
 {ACD} = {ACD}
+
 {ABCD} = {ABCD}

32
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
 CA, CD, DA

 ABC, ABD, ACD

 BCA, BCD, BDA, BDC,  CDA

 ABCD, ABDC, BCDA 

33
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
 CA, CD, DA

 ABC, ABD, ACD

 BCA, BCD, BDA, BDC,  CDA

 ABCD, ABDC, BCDA 

4. For each non‐trivial and decomposed FD, check 
whether its left hand side is a super‐key

34
BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


2. Derive the keys of R: AB, BC, BD
3. Derive the non‐trivial and decomposed FDs on R
 CA, CD, DA

 ABC, ABD, ACD Not in BCNF


 BCA, BCD, BDA, BDC,  CDA

 ABCD, ABDC, BCDA 

4. For each non‐trivial and decomposed FD, check 
whether its left hand side is a super‐key

35
BCNF Check
 The previous algorithm
 Compute the closure of each attribute subset
 Derive the keys of R (using closures)
 Derive all non‐trivial and decomposed FDs on R (again, 
using closures)
 Check the non‐trivial and decomposed FDs to see if they 
satisfy BCNF requirement
 If all of them satisfy the requirement, then R is in BCNF
 Observation:
 The three steps in blue are quite tedious
 We will simplify it by combing them together, again using 
closures

36
Simplified BCNF Check: How? 
 What we need: check if there is a non‐trivial and 
decomposed FD A1A2…AkB1, such that A1A2…Ak is not a 
superkey
 Question: if A1A2…Ak is not a superkey, what would its 
closure {A1A2…Ak}+ look like?
 First, the closure should contain B1, which is not in 
{A1A2…Ak}
 i.e., the closure contains more attributes than {A1A2…Ak} does
 Second, the closure should not contain all attributes in 
the table, since A1A2…Ak is not a superkey
 i.e., the closure contains not all attributes in the table
 Conclusion: we have a violation of BCNF, iff we have a 
closure that satisfies the "more but not all" condition

37
Simplified BCNF Check: Algorithm
 Conclusion: we have a violation of BCNF, iff we 
have a closure that satisfies the "more but not 
all" condition
 Simplified Algorithm for BCNF check:
 Compute the closure of each attribute subset
 Check if there is a closure {A1A2…Ak}+, such that
 The closure contains some attribute not in {A1A2…Ak}
 The closure does not contain all attributes in the table
 i.e., a "more but not all" closure
 If such a closure exists, then R is NOT in BCNF

38
Simplified BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA

39
Simplified BCNF Check: Example

 R(A, B, C, D) with FDs AB  C, C  D, and DA


1. Compute the closure of each attribute subset
 {A}+= {A},  {B}+= {B},  {C}+= {ACD},  {D}+= {AD}
 Stop right there…
 Take a look at {C}+= {ACD}
 {C}+ contains more attributes than {C} does
 {C}+ does not contain all attributes in R
 "More but not all", which is a violation of BCNF
 So R is not in BCNF

40
Exercise

 R(A, B, C, D) with FDs B  C, B  D
 Is R in BCNF?

41
Exercise
Not in BCNF
 R(A, B, C, D) with FDs B  C, B  D
 Compute the closure of each attribute subset
 {A}+= {A},  {B}+= {BCD},  {C}+= {C},  {D}+= {D}
 {B}+= {BCD} stratifies the "more but not all" property
 So it indicates a violation of BCNF

42
Exercise

 R(A, B, C, D) with FDs A  B, B  C, CD, and 


DA
 Is R in BCNF?

43
Exercise
In BCNF
 R(A, B, C, D) with FDs A  B, B  C, CD, and 
DA
 Compute the closure for each subset of the attributes 
in R
 {A}+= {ABCD},  {B}+= {ABCD},  {C}+= {ABCD},  {D}+= {ABCD}
 The other closures are all {ABCD}
 There is no closure satisfying the "more but not 
all" property
 So there is no violation of BCNF

44
Exercise

 R(A, B, C, D, E) with FDs AB  C, C  E, E  A, 


and E  D
 Is R in BCNF?

45
Exercise
Not In BCNF
 R(A, B, C, D, E) with FDs AB  C, C  E, E  A, 
and E  D
 Compute the closure for each subset of the attributes 
in R
 {A}+= {A},  {B}+= {B},  {C}+= {ACDE},  {D}+= {D} ,  {E}+= {ADE}
 {E}+= {ADE} satisfies the "more but not all" property
 So {E}+= {ADE} indicates a violation of BCNF

46
Exercise

 R(A, B, C, D) with FDs AB  D, BD  C, CD  A, 


and AC  B
 Is R in BCNF?

47
Exercise
In BCNF
 R(A, B, C, D) with FDs AB  D, BD  C, CD  A, and 
AC  B
 Compute the closure for each subset of the attributes in R
 {A}+= {A},  {B}+= {B},  {C}+= {C},  {D}+= {D}
 {AB}+= {BD}+= {CD}+= {AC}+= {ABCD},  
 {AD}+= {AD}, {BC}+= {BC}
 {ABC}+= {ABD}+= {BCD}+ = {ACD}+ = {ABCD} 
 {ABCD}+ = {ABCD} 
 There is no closure satisfying the "more but not all" 
property
 So there is no violation of BCNF

48
Roadmap

 Now we know how to check whether a table is in 
BCNF
 But if a table is not in BCNF, how can we improve 
it?
 We can decompose it into smaller tables
 This is also called a normalization

49
BCNF Decomposition
Name NRIC PhoneNumber HomeAddress
Alice 1234 67899876 Jurong East
Alice 1234 83848384 Jurong East
Bob 5678 98765432 Pasir Ris

 Decomposing non‐BCNF tables into smaller ones in 
BCNF
Name NRIC HomeAddress NRIC PhoneNumber
Alice 1234 Jurong East 1234 67899876
Bob 5678 Pasir Ris 1234 83848384
5678 98765432

50
Decompose, until all are in BCNF
Not in BCNF? 
Decompose 

Not in BCNF? 
Decompose 
In BCNF? 
Stop 

In BCNF?  In BCNF? 
Stop  Stop 
51
Algorithm {X}+ a violation R
X + Attributes 
NOT in {X}+
Attributes in {X}+
R1 R2

 Input: a table R
1. Find a subset X of the attributes in R, such that its 
closure {X}+ (i) contains more attributes than X 
does, but (ii) does not contain all attributes in R
2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in {X}+
 R2 contains all attributes in X as well as the attributes not 
in {X}+
3. If R1 is not in BCNF, further decompose R1;
If R2 is not in BCNF, further decompose R2

52
BCNF Decomposition: Example
Name NRIC PhoneNumber HomeAddress
Alice 1234 67899876 Jurong East
Alice 1234 83848384 Jurong East
Bob 5678 98765432 Pasir Ris
 FD: NRIC  Name, HomeAddress
1. Find a subset X of the attributes in R, such that its closure X+ (i) contains 
more attributes than X, but (ii) does not contain all attributes in R
 {NRIC}+ = {Name, NRIC, HomeAddress}
2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in X+
 R2 contains all attributes in X as well as the attributes not in X+
 R1(Name, NRIC, HomeAddress), R2(NRIC, PhoneNumber)
3. Check if R1 and R2 are in BCNF, and so on. (Spoiler: they are in BCNF)

53
BCNF Decomposition: Example
Name NRIC PhoneNumber HomeAddress
Alice 1234 67899876 Jurong East
Alice 1234 83848384 Jurong East
Bob 5678 98765432 Pasir Ris

Name NRIC HomeAddress NRIC PhoneNumber


Alice 1234 Jurong East 1234 67899876
Bob 5678 Pasir Ris 1234 83848384
5678 98765432

54
BCNF Decomposition: Example
 R(A, B, C, D) with FDs AB, BC
1. Find a subset X of the attributes in R, such that its closure 
{X}+ (i) contains more attributes than X, but (ii) does not 
contain all attributes in R
 {A}+ = {A, B, C}

2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in {X}+
 R2 contains all attributes in X as well as the attributes not in {X}+
 R1(A, B, C), R2(A, D)
3. Check if R1 and R2 are in BCNF
 R1: No, R2: Yes

4. Further decompose R1
55
BCNF Decomposition: Example
 R(A, B, C, D) with FDs AB, BC
 R1(A, B, C), R2(A, D)

 Further decompose R1

1. Find a subset X of the attributes in R1, such that its closure {X}+
(i) contains more attributes than X, but (ii) does not contain all 
attributes in R
 {A}+ = {A, B, C}, {B}+ = {B, C}

2. Decompose R1 into two tables R3 and R4, such that


 R3 contains all attributes in {X}+
 R4 contains all attributes in X as well as the attributes not in {X}+
 R3(B, C), R4(A, B)
3. Check if R1 and R2 are in BCNF
 R3: Yes, R4: Yes

 Final results: R3(B, C), R4(A, B), R2(A, D)

56
Decompose, until all are in BCNF
{X}+ violation? 
Decompose 
X + Attributes 
Attributes in {X}+ NOT in {X}+
{X’}+ violation? 
Decompose 
Attributes in {X’}+

In BCNF then  In BCNF then
Stop  Stop 
57
Notes

 The BCNF decomposition of a table may not be 
unique

 If a table has only two attributes, then it must be 
in BCNF
 Therefore, you do not need to check tables with only 
two attributes

58
BCNF Decomposition: One More Issue

 Recall that, whenever we decompose a table R 
into two smaller tables R1 and R2, we need to 
check whether R1 and R2 satisfies BCNF
 This requires us to check the closures on R1 and 
R2
 We will explain how this can be done using an 
example

59
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Step 1: Check if there is closure that 
indicates a violation of BCNF
 {A}+ = {A, B} R1 R2
 Step 2: Decompose the table into 
two A B A C D E
 First one: include all attributes in the 
closure, i.e, {A, B}
 Second one: include A and all 
attributes NOT in the closure, i.e., {A, 
C, D, E}
 Now we need to check whether R1
and R2 are in BCNF
 R1 is in BCNF; but what about R2?
60
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 To check whether R2 is in 
BCNF, we need to derive the R1 R2
closures for R2 A B A C D E
 But we don't know what FDs 
are there on R2
 Solution:
 Derive the closures on R

 Then, project them onto R2

61
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 1: enumerate the 
attribute subsets in R2 R1 R2
A B A C D E
 {A} {C}
 {D} {E}
 {AC} {AD}
 {AE} {CD}
 {CE} {DE}
 {ACD} {ACE}
 {ADE} {CDE}

62
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 2: derive the closures of 
these attribute subsets on R R1 R2
A B A C D E
 {A} {C}
 {D} {E}
 {AC} {AD}
 {AE} {CD}
 {CE} {DE}
 {ACD} {ACE}
 {ADE} {CDE}

63
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 2: derive the closures of 
these attribute subsets on R R1 R2
A B A C D E
 {A}+={AB} {C}+={C}
 {D}+={D} {E} +={E}
 {AC}+={ABCD} {AD}+={ABD}
 {AE} +={ABE} {CD} +={CD}
 {CE} +={CE} {DE}+={DE}
 {ACD}+={ABCD}  {ACE}+={ABCDE}
 {ADE}+={ABCDE} {CDE} +={CDE}

64
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 3: Project these closures 
onto R2, by removing irrelevant  R1 R2
attributes A B A C D E
 {A}+={AB} {C}+={C}
 {D}+={D} {E} +={E}
 {AC}+={ABCD} {AD}+={ABD}
 {AE} +={ABE} {CD} +={CD}
 {CE} +={CE} {DE}+={DE}
 {ACD}+={ABCD}  {ACE}+={ABCDE}
 {ADE}+={ABCDE} {CDE} +={CDE}

65
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 3: Project these closures 
onto R2, by removing irrelevant  R1 R2
attributes A B A C D E
 {A}+={AB} {C}+={C}
 {D}+={D} {E} +={E}
 {AC}+={ABCD} {AD}+={ABD}
 {AE} +={ABE} {CD} +={CD}
 {CE} +={CE} {DE}+={DE}
 {ACD}+={ABCD}  {ACE}+={ABCDE}
 {ADE}+={ABCDE} {CDE} +={CDE}

66
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 3: Project these closures 
onto R2, by removing irrelevant  R1 R2
attributes A B A C D E
 {A}+={A} {C}+={C}
 {D}+={D} {E} +={E}
 {AC}+={ACD} {AD}+={AD}
 {AE} +={AE} {CD} +={CD}
 {CE} +={CE} {DE}+={DE}
 {ACD}+={ACD}  {ACE}+={ACDE}
 {ADE}+={ACDE} {CDE} +={CDE}

67
BCNF Decomposition: One More Issue
 Given: AB, BCD R AB CD E
 Deriving closures for R2
 Step 3: Project these closures 
onto R2, by removing irrelevant  R1 R2
attributes A B A C D E
 {A}+={A} {C}+={C}
 {D}+={D} {E} +={E}
• This closure
 {AC}+={ACD} {AD}+={AD}
violates BCNF
 {AE} +={AE} {CD} +={CD} • So R2 is not
 {CE} +={CE} {DE}+={DE} in BCNF
 {ACD}+={ACD}  {ACE}+={ACDE}
 {ADE}+={ACDE} {CDE} +={CDE}

68
Projection of Closures/FDs
 In general, if we are to derive the closures on a 
table Ri that is decomposed from a table R, we 
can
 First, enumerate the attribute subsets of Ri
 For each subset, derive its closure on R
 Project each closure onto Ri by removing those 
attributes that do not appear in Ri
 These projected closures can then be used to 
 Decide whether Ri is in BCNF
 Further decompose Ri (if Ri violates BCNF)

69
Question

 Why does the BCNF decomposition algorithm 
work?
 Why can it eliminate violations of BCNF?

70
BCNF Decomposition Algorithm
XY violation? 
Decompose 
X + Attributes 
Attributes in {X}+ NOT in {X}+
X’Y’ violation? 
Decompose 
Attributes in {X’}+

In BCNF then  In BCNF then
Stop  Stop 
71
BCNF Decomposition Algorithm
XY violation?  R
Decompose 
X + Attributes 
Attributes in {X}+ NOT in {X}+

R1 R2

72
BCNF Decomposition Algorithm
XY violation?  R
Decompose 
X + Attributes 
Attributes in {X}+ NOT in {X}+

R1 R2

 XY is no longer a BCNF violation on R1
 XY is no longer an FD on R2
 So this decomposition step gets rid of one BCNF 
violation

73
BCNF Decomposition Algorithm
XY violation?  R
Decompose 
X + Attributes 
Attributes in {X}+ NOT in {X}+

R1 R2

 In general, each decomposition step removes at 
least one BCNF violation
 Recursive decomposition ==> all violations will be 
removed in the end
74
Exercise
 R(A, B, C, D) with FDs AB, AC
1. Find a subset X of the attributes in R, such that its 
closure X+ (i) contains more attributes than X, but 
(ii) does not contain all attributes in R
2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in X+
 R2 contains all attributes in X as well as the attributes not 
in X+
3. If R1 is not in BCNF, further decompose R1;
If R2 is not in BCNF, further decompose R2

75
Exercise
 R(A, B, C, D) with FDs AB, AC
1. Find a subset X of the attributes in R, such that its 
closure X+ (i) contains more attributes than X, but (ii) 
does not contain all attributes in R
 {A}+ = {A, B, C}
2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in X+
 R2 contains all attributes in X as well as the attributes not 
in X+
 R1(A, B, C), R2(A, D)
3. Check if R1 and R2 are in BCNF
 Yes. Final results: R1(A, B, C), R2(A, D)

76
Exercise
 R(A, B, C, D) with FDs BCD, DA, AB
1. Find a subset X of the attributes in R, such that its 
closure X+ (i) contains more attributes than X, but 
(ii) does not contain all attributes in R
2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in X+
 R2 contains all attributes in X as well as the attributes not 
in X+
3. If R1 is not in BCNF, further decompose R1;
If R2 is not in BCNF, further decompose R2

77
Exercise
 R(A, B, C, D) with FDs BCD, DA, AB
1. Find a subset X of the attributes in R, such that its closure 
X+ (i) contains more attributes than X, but (ii) does not 
contain all attributes in R
 {A}+ = {A, B}

2. Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in X+
 R2 contains all attributes in X as well as the attributes not in X+
 R1(A, B), R2(A, C, D)
3. Check if R1 and R2 are in BCNF
 R1: Yes. R2: No

 Further decompose R2

78
Exercise
 R(A, B, C, D) with FDs BCD, DA, AB
 R1(A, B), R2(A, C, D)
 Further decompose R2
1. Find a subset X of the attributes in R2, such that its 
closure X+ (i) contains more attributes than X, but (ii) does 
not contain all attributes in R2
 {A}+ = {A}, {C}+ = {C}, {D}+ = {A, D}, 
2. Decompose R1 into two tables R3 and R4, such that
 R3 contains all attributes in X+
 R4 contains all attributes in X as well as the attributes not in X+
 R3(A, D), R4(C, D)
3. Check if R3 and R4 are in BCNF
 Yes. Final results: R3(A, D), R4(A, C)

79
Properties of BCNF

 Good properties
 No update or deletion or insertion anomalies
 Small redundancy
 The original table can always be reconstructed from 
the decomposed tables

80
Table Reconstruction
Name NRIC PhoneNumber HomeAddress
Alice 1234 67899876 Jurong East
Alice 1234 83848384 Jurong East
Bob 5678 98765432 Pasir Ris

 SELECT * FROM R1, R2 This is called a 


WHERE R1.NRIC = R2.NRIC “Lossless Join”
Name NRIC HomeAddress NRIC PhoneNumber
Alice 1234 Jurong East 1234 67899876
Bob 5678 Pasir Ris 1234 83848384
R1 5678 98765432
R2 81
Lossless Join Decomposition

 Say we decompose a table R into two tables R1
and R2
 The decomposition guarantees lossless join, 
whenever the common attributes in R1 and R2
constitute a superkey of R1 or R2
 Example
 R(A, B, C) decomposed into R1(A, B) and R2 (B, C), with 
B being a superkey of R2
 R(A, B, C, D) decomposed into R1(A, B, C) and R2 (B, C, 
D), with BC being a superkey of R1

82
Why BCNF guarantees lossless join?
 Decompose R into two tables R1 and R2, such that
 R1 contains all attributes in {X}+
 R2 contains all attributes in X as well as the attributes not in {X}+
 Let Y = {X}+ ‐ X, and Z be the set of attributes not in {X}+

R X Y Z R1 X Y R2 X Z

 Suppose that we join R1 and R2 on the attributes in X


 For any tuple in R, it will appear in the join result
 For any tuple in the join result, it will appear in R
 Therefore, joining R1 and R2 on X will reconstruct R 
perfectly

83
Properties of BCNF

 Good properties
 No update or deletion or insertion anomalies
 Small redundancy
 The original table can always be reconstructed from 
the decomposed tables
 Bad properties
 Dependencies may not be preserved
 We will talk about it in the next lecture

84

You might also like