Top Banner
BCNF and Normalization BCNF and Normalization Zaki Malik October 21, 2008 October 21, 2008
27

BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Feb 16, 2018

Download

Documents

hoangmien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

BCNF and NormalizationBCNF and Normalization

Zaki Malik

October 21, 2008October 21, 2008

Page 2: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Relational Schema DesignRelational Schema Design

• Goal of relational schema design is to avoidGoal of relational schema design is to avoid redundancy and anomalies.

Page 3: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Bad DesignBad Design

name addr beersLiked manf favBeername addr beersLiked manf favBeer

Janeway Voyager Export Molson G.I. LagerJaneway Voyager G.I. Lager Gr. Is. G.I. LagerSpock Enterprise E port Molson E portSpock Enterprise Export Molson Export

•Redundancyy

• Update anomaly• if Janeway is transferred to Intrepid, will we remember to change each of her tuples?of her tuples?

• Deletion anomaly•If nobody likes Export, we lose track of the fact that Molson manufactures Export.

Page 4: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Another ExampleAnother Example

Page 5: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Relational DecompositionRelational Decomposition

Page 6: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Example of DecompositionExample of Decomposition

Page 7: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Triviality of FDs

7

Page 8: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Boyce‐Codd Normal FormBoyce Codd Normal Form

Page 9: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Closures of FDs vs. Closures of AttributesClosures of FDs vs. Closures of Attributes

Page 10: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Checking for BCNF Violations

Page 11: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Decomposition into BCNF

Page 12: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Decomposing CoursesDecomposing Courses

Page 13: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Decomposing Courses

Page 14: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Another Example of DecompositionAnother Example of Decomposition

Page 15: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Another Example of Decomposition (2)

Page 16: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

BCNFs and Two‐Attribute Relationships

Page 17: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Decomposition into BCNFDecomposition into BCNF

Page 18: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Candidate Normalization AlgorithmCandidate Normalization Algorithm

Page 19: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Joining RelationsJoining Relations

Page 20: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Recovering Information from a Decomposition

Page 21: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Correct Decompositions

A decomposition is lossless if we can recover:p

R(A,B,C)

Decompose

R1(A,B) R2(A,C)

Decompose

R1(A,B)      R2(A,C)

Recover

R’(A,B,C)   should be the same asR(A,B,C)R(A,B,C)

R’ is in general larger than R. Must ensure R’ = R

Page 22: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Example of Lossy‐Join Decomposition 

• Example: Decomposition of R = (A, B)R1 = (A) R2 = (B)

A B A B

ααβ

121

αβ

12

∏ ( ) ∏β

r∏A(r) ∏B(r)

∏A (r) ∏B (r) A B

ααββ

1212β 2

Page 23: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Example: BCNF DecompositionExample: BCNF Decomposition

Drinkers(name addr beersLiked manf favBeer)Drinkers(name, addr, beersLiked, manf, favBeer)FDs = name‐>addr,   name ‐> favBeer,   beersLiked‐>manf

• Pick BCNF violation name >addr• Pick BCNF violation name‐>addr.• Close the left side: {name}+ = {name, addr, 

favBeer}favBeer}.• Decomposed relations:

1 i k 1( dd f )1. Drinkers1(name, addr, favBeer)2. Drinkers2(name, beersLiked, manf)

Page 24: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Example ‐‐ ContinuedExample  Continued

• We are not done; we need to check Drinkers1 and Drinkers2 for BCNF.

• Is Drinkers1 in BCNF ?– For Drinkers1(name, addr, favBeer), relevant FD’s are name‐>addr and   name‐>favBeer.

– Thus, {name} is the only key and Drinkers1 is in BCNFBCNF.

Page 25: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Example ‐‐ ContinuedExample  Continued

• For Drinkers2(name beersLiked manf) the• For Drinkers2(name, beersLiked, manf), the only FD is   beersLiked‐>manf, and the only key iskey is 

{name, beersLiked}.Violation of BCNF ?– Violation of BCNF ? 

• beersLiked+ = {beersLiked, manf}, so we decompose Drinkers2 into:decompose Drinkers2 into:1. Drinkers3(beersLiked, manf)2 Drinkers4(name beersLiked)2. Drinkers4(name, beersLiked)

Page 26: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Example ‐‐ ConcludedExample  Concluded

• The resulting decomposition of Drinkers :The resulting decomposition of Drinkers :1. Drinkers1(name, addr, favBeer)

2 Drinkers3(beersLiked manf)2. Drinkers3(beersLiked, manf)

3. Drinkers4(name, beersLiked)

• Note: D i k 1 t ll b t d i k– Drinkers1 tells us about drinkers,

– Drinkers3 tells us about beers, and 

– Drinkers4 tells us the relationship between drinkers andDrinkers4 tells us the relationship between drinkers and the beers they like.

Page 27: BCNF and Normalization - Computer Science atcourses.cs.vt.edu/~cs4604/Fall08/lectures/lecture14.pdf · BCNF and Normalization Zaki Malik October 21, 2008. Relational Schema Design

Summary of BCNF DecompositionSummary of BCNF Decomposition

Find a dependency that violates the BCNF condition:

A , A , … A 1 2 n B , B , … B

1 2 m

Decompose:

A’sOthers B’sIs there a 2 attribute

Continue untilthere are noBCNF violationsleft

R1 R2

2-attribute relation that isnot in BCNF ?

left.