Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


Presentation on theme: "CPSC-310 Database Systems"— Presentation transcript:

1 CPSC-310 Database Systems
Professor Jianer Chen Room 315C HRBB Lecture #7

2 Boyce-Codd Normal Form (BCNF)
Definition. A relation R is in Boyce-Codd Normal Form (BCNF) if every nontrivial FD X→A (i.e., AX) has its left side X a superkey.

3 Decomposition into BCNF
Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) has (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C;

4 Why Does the Algorithm BCNF Work?

5 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes.

6 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes. Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2.

7 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes. Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2.

8 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) and (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2.

9 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) and (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2.

10 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes. A relation with only two attributes is always in BCNF Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) and (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2.

11 Why Does the Algorithm BCNF Work?
Reasons: The algorithm Decomposition(X) always breaks a relation R into two relations with fewer attributes. A relation with only two attributes is always in BCNF Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) and (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2.

12 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)}

13 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R

14 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}.

15 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) R2 = Drinkers2(name, beersLiked, manf)

16 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) R2 = Drinkers2(name, beersLiked, manf) Add R1 and R2 to C’; C’ = {(R1, T1), (R2, T2)}

17 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) R2 = Drinkers2(name, beersLiked, manf) Add R1 and R2 to C’; C’ = {(R1, T1), (R2, T2)} We are not done, yet since C’  ;

18 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) R2 = Drinkers2(name, beersLiked, manf) Add R1 and R2 to C’; C’ = {(R1, T1), (R2, T2)} We are not done, yet since C’  ; Pick R1 = Drinkers1(name, addr, favBeer), with the FD’s T1= { name→addr, name→favBeer}.

19 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) R2 = Drinkers2(name, beersLiked, manf) Add R1 and R2 to C’; C’ = {(R1, T1), (R2, T2)} We are not done, yet since C’  ; Pick R1 = Drinkers1(name, addr, favBeer), with the FD’s T1= { name→addr, name→favBeer}. {name} is the only key so R1 = Drinkers1 is in BCNF.

20 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) Add R1 and R2 to C’; C’ = {(R1, T1), (R2, T2)} We are not done, yet since C’  ; Pick R1 = Drinkers1(name, addr, favBeer), with the FD’s T1= { name→addr, name→favBeer}. {name} is the only key so R1 = Drinkers1 is in BCNF.

21 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) C’ = {(R2, T2)}

22 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) Pick R2= Drinkers2(name, beersLiked, manf) from C’ with FDs T2= { beersLiked→manf}, the only key is {name, beersLiked}

23 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) Pick R2= Drinkers2(name, beersLiked, manf) from C’ with FDs T2= { beersLiked→manf}, the only key is {name, beersLiked} So beersLiked→manf is a BCNF violator.

24 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) Pick R2= Drinkers2(name, beersLiked, manf) from C’ with FDs T2= { beersLiked→manf}, the only key is {name, beersLiked} So beersLiked→manf is a BCNF violator. {beersLiked}+ = {beersLiked, manf}.

25 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) Pick R2= Drinkers2(name, beersLiked, manf) from C’ with FDs T2= { beersLiked→manf}, the only key is {name, beersLiked} So beersLiked→manf is a BCNF violator. {beersLiked}+ = {beersLiked, manf}. Decompose Drinkers2: R3= Drinkers3(beersLiked, manf), T3 = {beersLiked→manf} R4= Drinkers4(name, beersLiked), no nontrivial FD

26 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) Pick R2= Drinkers2(name, beersLiked, manf) from C’ with FDs T2= { beersLiked→manf}, the only key is {name, beersLiked} So beersLiked→manf is a BCNF violator. {beersLiked}+ = {beersLiked, manf}. Decompose Drinkers2: R3= Drinkers3(beersLiked, manf), T3 = {beersLiked→manf} R4= Drinkers4(name, beersLiked), no nontrivial FD Add R3, R4 to C’. They are in BCNF, so will be moved to C.

27 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) The resulting decomposition of Drinkers: R1 = Drinkers1(name, addr, favBeer) R3 = Drinkers3(beersLiked, manf) R4 = Drinkers4(name, beersLiked)

28 Example R = Drinkers(name, addr, beersLiked, manf, favBeer)
T = {name→addr, name→favBeer, beersLiked→manf} The only key is {name, beersLiked}, C’ = {(R, T)} Pick (R, T) from C’, and pick a BCNF violation name→addr in R Close the left side: {name}+ = {name, addr, favBeer}. Decomposed relations: R1 = Drinkers1(name, addr, favBeer) in BCNF , add to C R2 = Drinkers2(name, beersLiked, manf) The resulting decomposition of Drinkers: R1 = Drinkers1(name, addr, favBeer) R3 = Drinkers3(beersLiked, manf) R4 = Drinkers4(name, beersLiked) Notice: Drinkers1 tells about drinkers, Drinkers3 tells about beers, and Drinkers4 tells the relationship between drinkers and the beers they like.

29 Boyce-Codd Normal Form (BCNF)
Definition. A relation R is in Boyce-Codd Normal Form (BCNF) if every nontrivial FD X→A (i.e., AX) has its left side X a superkey. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) has (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C;

30 Boyce-Codd Normal Form (BCNF)
Definition. A relation R is in Boyce-Codd Normal Form (BCNF) if every nontrivial FD X→A (i.e., AX) has its left side X a superkey. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) has (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Remarks. Algorithm constructs relations in BCNF.

31 Boyce-Codd Normal Form (BCNF)
Definition. A relation R is in Boyce-Codd Normal Form (BCNF) if every nontrivial FD X→A (i.e., AX) has its left side X a superkey. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) has (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Remarks. Algorithm constructs relations in BCNF. No information in the relation R is changed by the algorithm.

32 Boyce-Codd Normal Form (BCNF)
Definition. A relation R is in Boyce-Codd Normal Form (BCNF) if every nontrivial FD X→A (i.e., AX) has its left side X a superkey. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) has (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Remarks. Algorithm constructs relations in BCNF. No information in the relation R is changed by the algorithm. This is because the algorithm Decomposition does not change information

33 Eliminating bad FD X → A (for all A)
Remarks. Algorithm Decomposition(X) eliminates the bad FD X→A: * X→A is still an FD in R1=(X+) but now X is a superkey for R1; * X→A is not FD in R2=(X∪W) because A is not in X∪W. Does Decomposition(X) change (i.e., lose or add extra) information for R: No. Can R1 and R2 still have bad FDs: Yes. Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2. Algorithm Decomposed-FDs(R1) 1. T1 = ; \\ T1 is the FDs for R1 2. For each subset Y of S1 Do 2.1 compute Y+ using the FDs in R; 2.2 For each A in S1∩(Y+\Y) add Y → A to T1; 3. While changes Do 3.1 Drop from T1 those FDs that are derivable from the others; 3.2 For each XB → A in T1, if X → A is implied by T1, then replace XB → A in T1 by X → A.

34 Eliminating bad FD X → A (for all A)
Remarks. Algorithm Decomposition(X) eliminates the bad FD X→A: * X→A is still an FD in R1=(X+) but now X is a superkey for R1; * X→A is not FD in R2=(X∪W) because A is not in X∪W. Does Decomposition(X) change (i.e., lose or add extra) information for R: No. Can R1 and R2 still have bad FDs: Yes. However, Decomposition(X) may not preserve FDs in R !! Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2. Algorithm Decomposed-FDs(R1) 1. T1 = ; \\ T1 is the FDs for R1 2. For each subset Y of S1 Do 2.1 compute Y+ using the FDs in R; 2.2 For each A in S1∩(Y+\Y) add Y → A to T1; 3. While changes Do 3.1 Drop from T1 those FDs that are derivable from the others; 3.2 For each XB → A in T1, if X → A is implied by T1, then replace XB → A in T1 by X → A.

35 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B}

36 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code.

37 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. There are two keys, {A,B} and {A,C}.

38 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. There are two keys, {A,B} and {A,C}. So C→B is a BCNF violation.

39 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. There are two keys, {A,B} and {A,C}. So C→B is a BCNF violation. The algorithm Decomposition breaks R(A,B,C) into R1(A,C) with no nontrivial FDs, and R2(B,C) with FDs {C→B}.

40 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. There are two keys, {A,B} and {A,C}. So C→B is a BCNF violation. The algorithm Decomposition breaks R(A,B,C) into R1(A,C) with no nontrivial FDs, and R2(B,C) with FDs {C→B}. However, neither R1 nor R2 can enforce AB→C because neither contains all A, B, C.

41 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. R1(A,C) with FDs {} and R2(B,C) with FDs {C→B}.

42 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. R1(A,C) with FDs {} and R2(B,C) with FDs {C→B}. Valid R1(A,C) Valid R2(B,C) Street Addr Zip Code 545 Tech Sq. 02138 02139 City Zip Code Cambridge 02138 02139

43 Decomposition Does not Preserve FDs
Example. Relation R(A,B,C) with FDs = {AB→C, C→B} e.g., A = street address, B = city, C = zip code. R1(A,C) with FDs {} and R2(B,C) with FDs {C→B}. Valid R1(A,C) Valid R2(B,C) Street Addr Zip Code 545 Tech Sq. 02138 02139 City Zip Code Cambridge 02138 02139 However, the natural join R1(A,C)⋈R2(B,C) is invalid for R(A,B,C) Street Addr City Zip Cold 545 Tech Sq. Cambridge 02138 02139 {Street Addr, City} → Zip (i.e., AB → C) is violated.

44 Eliminating bad FD X → A (for all A)
Remarks. Algorithm Decomposition(X) eliminates the bad FD X→A: * X→A is still an FD in R1=(X+) but now X is a superkey for R1; * X→A is not FD in R2=(X∪W) because A is not in X∪W. Does Decomposition(X) change (i.e., lose or add extra) information for R: No. Can R1 and R2 still have bad FDs: Yes. However, Decomposition(X) may not preserve FDs in R !! Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2. Algorithm Decomposed-FDs(R1) 1. T1 = ; \\ T1 is the FDs for R1 2. For each subset Y of S1 Do 2.1 compute Y+ using the FDs in R; 2.2 For each A in S1∩(Y+\Y) add Y → A to T1; 3. While changes Do 3.1 Drop from T1 those FDs that are derivable from the others; 3.2 For each XB → A in T1, if X → A is implied by T1, then replace XB → A in T1 by X → A.

45 Eliminating bad FD X → A (for all A)
Remarks. Algorithm Decomposition(X) eliminates the bad FD X→A: * X→A is still an FD in R1=(X+) but now X is a superkey for R1; * X→A is not FD in R2=(X∪W) because A is not in X∪W. Does Decomposition(X) change (i.e., lose or add extra) information for R: No. Can R1 and R2 still have bad FDs: Yes. However, Decomposition(X) may not preserve FDs in R !! Algorithm Decomposition(X) 1. Compute S1 = X+ using the FDs in R; 2. Let W be the set of attributes that are not in S1; 3. Make a relation R1 of schema S1; 4. Make a relation R2 with schema S2 = X ∪ W; 5. Compute the FDs for R1 and R2. Algorithm Decomposed-FDs(R1) 1. T1 = ; \\ T1 is the FDs for R1 2. For each subset Y of S1 Do 2.1 compute Y+ using the FDs in R; 2.2 For each A in S1∩(Y+\Y) add Y → A to T1; 3. While changes Do 3.1 Drop from T1 those FDs that are derivable from the others; 3.2 For each XB → A in T1, if X → A is implied by T1, then replace XB → A in T1 by X → A. This leads to

46 Boyce-Codd Normal Form (BCNF)
Definition. A relation R is in Boyce-Codd Normal Form (BCNF) if every nontrivial FD X→A (i.e., AX) has its left side X a superkey. Algorithm BCNF(R, T) Input: A relation R and its FDs T Output: A collection C of relations in BCNF 1. C = ; C’ = {(R, T)}; 2. While C’   Do 2.1 Pick (R’, T’) in C’ (and remove it from C’); 2.2 If (R’, T’) has a BCNF violator X; 2.3 Then Call Decomposition(X) to construct the two relations (R1, T1) has (R2, T2); add (R1, T1) and (R2, T2) to C’; 2.4 Else add (R’, T’) to C; Remarks. Algorithm constructs relations in BCNF. No information in the relation R is changed by the algorithm. The algorithm BCNF may not preserve FDs !!

47 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted

48 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs.

49 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key.

50 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime.

51 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime. * R is in BCNF ⟹ R is in 3NF

52 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime. * R is in BCNF ⟹ R is in 3NF * X→A violates 3NF ⟹ X is not a superkey & A is not a prime.

53 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime. Re-look at the example: R(A,B,C) with FDs {AB→C, C→B}.

54 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime. Re-look at the example: R(A,B,C) with FDs {AB→C, C→B}. R has keys AB and AC:

55 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime. Re-look at the example: R(A,B,C) with FDs {AB→C, C→B}. R has keys AB and AC: R is not in BCNF because of C→B.

56 The 3rd Normal Form (3NF) Thus, BCNF may be too restricted
We are looking for a constraint that may be less restricted but preserves FDs. An attribute A is a prime if A is in some key. The 3rd Normal Form (3NF) A relation R is in 3NF if for every nontrivial FD X→A of R, either X is a superkey or A is a prime. Re-look at the example: R(A,B,C) with FDs {AB→C, C→B}. R has keys AB and AC: R is not in BCNF because of C→B. However, R is in 3NF because in C→B, B is a prime

57 Recoverability and Dependency Preserving
Two important properties of a decomposition: Recoverability: The original relations can be reconstructed from the decomposed relations. Dependency Preservation: The FDs in the original relation can be derived based on the FDs in the decomposed relations.

58 Decomposition into 3NF Algorithm 3NF(R, T)
Input: A relation R and its FDs T Output: A collection C of relations in 3NF 1. While changes Do 1.1 If X→A in T is implied by the other FDs in T Then remove X→A from T; 1.2 If XB→A is in T and X→A is implied by T Then replace XB→A in T by X→A; 2. For each X→A in T Do construct a table RX,A = X→A(R) with FD {X→A}; 3. If none of the relations in step 2 contains a key Then add another relation R’ whose schema is a key and with no non-trivial FDs.

59 Decomposition into 3NF Algorithm 3NF(R, T)
Input: A relation R and its FDs T Output: A collection C of relations in 3NF 1. While changes Do 1.1 If X→A in T is implied by the other FDs in T Then remove X→A from T; 1.2 If XB→A is in T and X→A is implied by T Then replace XB→A in T by X→A; 2. For each X→A in T Do construct a table RX,A = X→A(R) with FD {X→A}; 3. If none of the relations in step 2 contains a key Then add another relation R’ whose schema is a key and with no non-trivial FDs.

60 Decomposition into 3NF Algorithm 3NF(R, T)
Input: A relation R and its FDs T Output: A collection C of relations in 3NF 1. While changes Do 1.1 If X→A in T is implied by the other FDs in T Then remove X→A from T; 1.2 If XB→A is in T and X→A is implied by T Then replace XB→A in T by X→A; 2. For each X→A in T Do construct a table RX,A = X→A(R) with FD {X→A}; 3. If none of the relations in step 2 contains a key Then add another relation R’ whose schema is a key and with no non-trivial FDs.

61 Decomposition into 3NF Algorithm 3NF(R, T)
Input: A relation R and its FDs T Output: A collection C of relations in 3NF 1. While changes Do 1.1 If X→A in T is implied by the other FDs in T Then remove X→A from T; 1.2 If XB→A is in T and X→A is implied by T Then replace XB→A in T by X→A; 2. For each X→A in T Do construct a table RX,A = X→A(R) with FD {X→A}; 3. If none of the relations in step 2 contains a key Then add another relation R’ whose schema is a key and with no non-trivial FDs.

62 Decomposition into 3NF Algorithm 3NF(R, T)
Input: A relation R and its FDs T Output: A collection C of relations in 3NF 1. While changes Do 1.1 If X→A in T is implied by the other FDs in T Then remove X→A from T; 1.2 If XB→A is in T and X→A is implied by T Then replace XB→A in T by X→A; 2. For each X→A in T Do construct a table RX,A = X→A(R) with FD {X→A}; 3. If none of the relations in step 2 contains a key Then add another relation R’ whose schema is a key and with no non-trivial FDs. It can be formally proved that the algorithm 3NF produces a set of relations in 3NF, which both is recoverable and FD preserving

63 Summary Two important properties of a decomposition:
Recoverability: The original relations can be reconstructed from the decomposed relations. Dependency Preservation: The FDs in the original relation can be derived based on the FDs in the decomposed relations.

64 Summary Two important properties of a decomposition:
Recoverability: The original relations can be reconstructed from the decomposed relations. Dependency Preservation: The FDs in the original relation can be derived based on the FDs in the decomposed relations. BCNF decomposition is recoverable but not necessarily FD preserving

65 Summary Two important properties of a decomposition:
Recoverability: The original relations can be reconstructed from the decomposed relations. Dependency Preservation: The FDs in the original relation can be derived based on the FDs in the decomposed relations. BCNF decomposition is recoverable but not necessarily FD preserving 3NF decomposition is both recoverable and FD preserving

66 Summary Two important properties of a decomposition:
Recoverability: The original relations can be reconstructed from the decomposed relations. Dependency Preservation: The FDs in the original relation can be derived based on the FDs in the decomposed relations. BCNF decomposition is recoverable but not necessarily FD preserving 3NF decomposition is both recoverable and FD preserving (however, it may introduce redundancy)


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google