Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.

Similar presentations


Presentation on theme: "Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd."— Presentation transcript:

1 Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd Normal Form Boyce-Codd Normal Form Third Normal Form Third Normal Form

2 Goals of Normalization Decide whether a particular relation R is in “good” form. Decide whether a particular relation R is in “good” form. In the case that a relation R is not in “good” form, decompose it into a set of relations {R 1, R 2,..., R n } such that In the case that a relation R is not in “good” form, decompose it into a set of relations {R 1, R 2,..., R n } such that the decomposition is a lossless-join decomposition the decomposition is a lossless-join decomposition each relation is in good form each relation is in good form

3 Decomposition Decompose the relation schema Lending-schema into: Decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city,assets) Loan-info-schema = (customer-name, loan-number, branch-name, amount) All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): R = R 1  R 2 Lossless-join decomposition. For all possible relations r on schema R Lossless-join decomposition. For all possible relations r on schema R r =  R1 (r)  R2 (r)

4 Example of Lossy-Join Decomposition Lossy-join decompositions result in information loss. Lossy-join decompositions result in information loss. Example: Decomposition of R = (A, B) R 1 = (A)R 2 = (B) Example: Decomposition of R = (A, B) R 1 = (A)R 2 = (B) AB  121121 A  B 1212 r A(r)A(r)  B(r)  A (r)  B (r) AB  12121212

5 A decomposition of R into R 1 and R 2 is lossless join if and only if at least one of the following dependencies is in F + : A decomposition of R into R 1 and R 2 is lossless join if and only if at least one of the following dependencies is in F + : R 1  R 2  R 1 R 1  R 2  R 1or R 1  R 2  R 2 R 1  R 2  R 2

6 Relation is in good form If the relations R i preferably should be in either Boyce-Codd Normal Form (BCNF) or Third (3NF) Normal Form If the relations R i preferably should be in either Boyce-Codd Normal Form (BCNF) or Third (3NF) Normal Form BCNF and 3NF eliminate redundancy BCNF and 3NF eliminate redundancy What is BCNF? What is BCNF? What is 3NF? What is 3NF?

7 Boyce-Codd Normal Form     is trivial (i.e.,    )     is trivial (i.e.,    )  is a superkey for R  is a superkey for R A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F + of the form    , where   R and   R, at least one of the following holds:

8 Example R = (A, B, C) F = {A  B, B  C} Key = {A} R = (A, B, C) F = {A  B, B  C} Key = {A} R is not in BCNF R is not in BCNF Decomposition R 1 = (A, B), R 2 = (B, C) Decomposition R 1 = (A, B), R 2 = (B, C) R 1 and R 2 in BCNF R 1 and R 2 in BCNF Lossless-join decomposition Lossless-join decomposition

9 Testing for BCNF To check if a non-trivial dependency     causes a violation of BCNF To check if a non-trivial dependency     causes a violation of BCNF 1. compute  + (the attribute closure of  ), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F +. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F +. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F + will cause a violation of BCNF either. If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in F + will cause a violation of BCNF either.

10 However, using only F is incorrect when testing a relation in a decomposition of R However, using only F is incorrect when testing a relation in a decomposition of R E.g. Consider R (A, B, C, D), with F = { A  B, B  C} E.g. Consider R (A, B, C, D), with F = { A  B, B  C} Decompose R into R 1 (A,B) and R 2 (A,C,D) Decompose R into R 1 (A,B) and R 2 (A,C,D) Neither of the dependencies in F contain only attributes from (A,C,D) so we might be mislead into thinking R 2 satisfies BCNF. Neither of the dependencies in F contain only attributes from (A,C,D) so we might be mislead into thinking R 2 satisfies BCNF. In fact, dependency A  C in F + shows R 2 is not in BCNF. In fact, dependency A  C in F + shows R 2 is not in BCNF.

11 BCNF Decomposition Algorithm result := {R}; done := false; compute F + ; while (not done) do if (there is a schema R i in result that is not in BCNF) then begin let     be a nontrivial functional dependency that holds on R i such that    R i is not in F +, and    =  ; result := (result – R i )  (R i –  )  ( ,  ); end else done := true; dependency that holds on R i such that    R i is not in F +, and    =  ; result := (result – R i )  (R i –  )  ( ,  ); end else done := true;

12 Testing if Ri is in BCNF To check if a relation R i in a decomposition of R is in BCNF To check if a relation R i in a decomposition of R is in BCNF Use the original set of dependencies F that hold on R, but with the following test: Use the original set of dependencies F that hold on R, but with the following test: for every set of attributes   R i, check that  + (the attribute closure of  ) either includes no attribute of R i - , or includes all attributes of R i. for every set of attributes   R i, check that  + (the attribute closure of  ) either includes no attribute of R i - , or includes all attributes of R i. If the condition is violated by some     in F, the dependency    (  + -   )  R i can be shown to hold on R i, and R i violates BCNF. If the condition is violated by some     in F, the dependency    (  + -   )  R i can be shown to hold on R i, and R i violates BCNF. We use above dependency to decompose R i We use above dependency to decompose R i

13 Example of BCNF Decomposition R = (branch-name, branch-city, assets, R = (branch-name, branch-city, assets, customer-name, loan-number, amount) F = {branch-name  assets, branch-city loan-number  amount, branch-name} Key = {loan-number, customer-name} Given: loan-number  branch-name, amount loan-number  branch-name, amount branch-name  branch-city, assets branch-name  branch-city, assets customer-name,loan-number  Lending-schema customer-name,loan-number  Lending-schema Decomposition Decomposition R 1 = (branch-name, branch-city, assets) R 1 = (branch-name, branch-city, assets) R 2 = (branch-name, customer-name, loan-number, amount) R 2 = (branch-name, customer-name, loan-number, amount) R 3 = (branch-name, loan-number, amount) R 3 = (branch-name, loan-number, amount) R 4 = (customer-name, loan-number) R 4 = (customer-name, loan-number) Final decomposition R 1, R 3, R 4 Final decomposition R 1, R 3, R 4

14 Additional Constraint of Decomposition Dependency preservation: Let F i be the set of dependencies F + that include only attributes in R i. Dependency preservation: Let F i be the set of dependencies F + that include only attributes in R i. Preferably the decomposition should be dependency preserving, that is, (F 1  F 2  …  F n ) + = F + Preferably the decomposition should be dependency preserving, that is, (F 1  F 2  …  F n ) + = F + Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive. Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive.

15 Example R = (A, B, C), F = {A  B, B  C) R = (A, B, C), F = {A  B, B  C) Can be decomposed in two different ways Can be decomposed in two different ways R 1 = (A, B), R 2 = (B, C) R 1 = (A, B), R 2 = (B, C) Lossless-join decomposition: Lossless-join decomposition: R 1  R 2 = {B} and B  BC R 1  R 2 = {B} and B  BC Dependency preserving Dependency preserving R 1 = (A, B), R 2 = (A, C) R 1 = (A, B), R 2 = (A, C) Lossless-join decomposition: Lossless-join decomposition: R 1  R 2 = {A} and A  AB R 1  R 2 = {A} and A  AB Not dependency preserving (cannot check B  C without computing R 1 R 2 ) Not dependency preserving (cannot check B  C without computing R 1 R 2 )

16 Testing for Dependency Preservation To check if a dependency  is preserved in a decomposition of R into R 1, R 2, …, R n we apply the following simplified test (with attribute closure done w.r.t. F) To check if a dependency  is preserved in a decomposition of R into R 1, R 2, …, R n we apply the following simplified test (with attribute closure done w.r.t. F) result =  while (changes to result) do for each R i in the decomposition t = (result  R i ) +  R i result = result  t result =  while (changes to result) do for each R i in the decomposition t = (result  R i ) +  R i result = result  t If result contains all attributes in , then the functional dependency    is preserved. If result contains all attributes in , then the functional dependency    is preserved.

17 BCNF and Dependency Preservation R = (J, K, L) F = {JK  L, L  K} Two candidate keys = JK and JL R = (J, K, L) F = {JK  L, L  K} Two candidate keys = JK and JL R is not in BCNF R is not in BCNF Any decomposition of R will fail to preserve JK  L Any decomposition of R will fail to preserve JK  L It is not always possible to get a BCNF decomposition that is dependency preserving

18 Third Normal Form: Motivation There are some situations where There are some situations where BCNF is not dependency preserving, and BCNF is not dependency preserving, and efficient checking for FD violation on updates is important efficient checking for FD violation on updates is important Solution: define a weaker normal form, called Third Normal Form. Solution: define a weaker normal form, called Third Normal Form. Allows some redundancy (with resultant problems; we will see examples later) Allows some redundancy (with resultant problems; we will see examples later) But FDs can be checked on individual relations without computing a join. But FDs can be checked on individual relations without computing a join. There is always a lossless-join, dependency- preserving decomposition into 3NF. There is always a lossless-join, dependency- preserving decomposition into 3NF.

19 Third Normal Form A relation schema R is in third normal form (3NF) if for all: A relation schema R is in third normal form (3NF) if for all:    in F + at least one of the following holds:    is trivial (i.e.,    )    is trivial (i.e.,    )  is a superkey for R  is a superkey for R Each attribute A in  –  is contained in a candidate key for R. Each attribute A in  –  is contained in a candidate key for R. (NOTE: each attribute may be in a different candidate key) (NOTE: each attribute may be in a different candidate key)

20 If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later). Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later).

21 Example R = (J, K, L) F = {JK  L, L  K} R = (J, K, L) F = {JK  L, L  K} Two candidate keys: JK and JL Two candidate keys: JK and JL R is in 3NF R is in 3NF JK  LJK is a superkey L  KK is contained in a candidate key n BCNF decomposition has (JL) and (LK) n Testing for JK  L requires a join There is some redundancy in this schema There is some redundancy in this schema

22 Testing for 3NF Optimization: Need to check only FDs in F, need not check all FDs in F +. Optimization: Need to check only FDs in F, need not check all FDs in F +. Use attribute closure to check for each dependency   , if  is a superkey. Use attribute closure to check for each dependency   , if  is a superkey. If  is not a superkey, we have to verify if each attribute in  is contained in a candidate key of R If  is not a superkey, we have to verify if each attribute in  is contained in a candidate key of R testing for 3NF has been shown to be NP-hard testing for 3NF has been shown to be NP-hard Interestingly, decomposition into third normal form (described shortly) can be done in polynomial time Interestingly, decomposition into third normal form (described shortly) can be done in polynomial time

23 3NF Decomposition Algorithm Let F c be a canonical cover for F; i := 0; for each functional dependency    in F c do if none of the schemas R j, 1  j  i contains   then begin i := i + 1; R i :=   end if none of the schemas R j, 1  j  i contains a candidate key for R then begin i := i + 1; R i := any candidate key for R; end return (R 1, R 2,..., R i )

24 3NF Decomposition Algorithm Above algorithm ensures: Above algorithm ensures: each relation schema R i is in 3NF each relation schema R i is in 3NF decomposition is dependency preserving and lossless- join decomposition is dependency preserving and lossless- join We skip the proof of correctness We skip the proof of correctness

25 Example Relation schema: Relation schema: Banker-info-schema = (branch-name, customer- name, banker-name, office-number) The functional dependencies for this relation schema are: banker-name  branch-name,office-number customer-name, branch-name  banker-name The functional dependencies for this relation schema are: banker-name  branch-name,office-number customer-name, branch-name  banker-name The key is: The key is: {customer-name, branch-name}

26 Applying 3NF to Banker-info- schema The for loop in the algorithm causes us to include the following schemas in our decomposition: The for loop in the algorithm causes us to include the following schemas in our decomposition: Banker-office-schema = (banker-name, branch- name, office-number) Banker-schema = (customer-name, branch-name, banker-name) Since Banker-schema contains a candidate key for Banker-info-schema, we are done Since Banker-schema contains a candidate key for Banker-info-schema, we are done

27 Comparison of BCNF and 3NF It is always possible to decompose a relation into relations in 3NF and It is always possible to decompose a relation into relations in 3NF and the decomposition is lossless the decomposition is lossless the dependencies are preserved the dependencies are preserved It is always possible to decompose a relation into relations in BCNF and It is always possible to decompose a relation into relations in BCNF and the decomposition is lossless the decomposition is lossless it may not be possible to preserve dependencies. it may not be possible to preserve dependencies.

28 Comparison of BCNF and 3NF J j 1 j 2 j 3 null L l1l1l1l2l1l1l1l2 K k1k1k1k2k1k1k1k2 A schema that is in 3NF but not in BCNF has the problems of repetition of information (e.g., the relationship l 1, k 1 ) need to use null values (e.g., to represent the relationship l 2, k 2 where there is no corresponding value for J). Example of problems due to redundancy in 3NF Example of problems due to redundancy in 3NF R = (J, K, L) F = {JK  L, L  K} R = (J, K, L) F = {JK  L, L  K}

29 Design Goals Goal for a relational database design is: Goal for a relational database design is: BCNF. BCNF. Lossless join. Lossless join. Dependency preservation. Dependency preservation. If we cannot achieve this, we accept one of If we cannot achieve this, we accept one of Lack of dependency preservation Lack of dependency preservation Redundancy due to use of 3NF Redundancy due to use of 3NF


Download ppt "Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd."

Similar presentations


Ads by Google