' & $ Chapter 7: Relational Database Design · Pitfalls in Relational Database Design · Decomposition · Normalization Using Functional Dependencies · Normalization Using Multivalued Dependencies · Normalization Using Join Dependencies · Domain-Key Normal Form · Alternative Approaches to Database Design Database Systems Concepts 7.1 Silberschatz, Korth and Sudarshan c 1997 ' & $ Pitfalls in Relational Database Design · Relational database design requires that we find a "good" collection of relation schemas. A bad design may lead to ­ Repetition of information. ­ Inability to represent certain information. · Design Goals: ­ Avoid redundant data ­ Ensure that relationships among attributes are represented ­ Facilitate the checking of updates for violation of database integrity constraints Database Systems Concepts 7.2 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · Consider the relation schema: Lending-schema = (branch-name, branch-city, assets, customer-name, loan-number, amount) · Redundancy: ­ Data for branch-name, branch-city, assets are repeated for each loan that a branch makes ­ Wastes space and complicates updating · Null values ­ Cannot store information about a branch if no loans exist ­ Can use null values, but they are difficult to handle Database Systems Concepts 7.3 Silberschatz, Korth and Sudarshan c 1997 ' & $ Decomposition · Decompose the relation schema Lending-schema into: Branch-customer-schema = (branch-name, branch-city, assets, customer-name) Customer-loan-schema = (customer-name, loan-number, amount) · All attributes of an original schema (R) must appear in the decomposition (R1, R2): R = R1 R2 · Lossless-join decomposition. For all possible relations r on schema R r = R1 (r) 1 R2 (r) Database Systems Concepts 7.4 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example of a Non Lossless-Join Decomposition · Decomposition of R = (A, B) R1 = (A) R2 = (B) A B A B 1 1 2 2 1 r A (r) B (r) · A (r) 1 B (r) A B 1 2 1 2 Database Systems Concepts 7.5 Silberschatz, Korth and Sudarshan c 1997 ' & $ Goal -- Devise a Theory for the Following: · 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 {R1, R2, ..., Rn} such that ­ each relation is in good form ­ the decomposition is a lossless-join decomposition · Our theory is based on: ­ functional dependencies ­ multivalued dependencies Database Systems Concepts 7.6 Silberschatz, Korth and Sudarshan c 1997 ' & $ Normalization Using Functional Dependencies When we decompose a relation schema R with a set of functional dependencies F into R1 and R2 we want: · Lossless-join decomposition: At least one of the following dependencies is in F+: ­ R1 R2 R1 ­ R1 R2 R2 · No redundancy: The relations R1 and R2 preferably should be in either Boyce-Codd Normal Form or Third Normal Form. · Dependency preservation: Let Fi be the set of dependencies in F+ that include only attributes in Ri . Test to see if: ­ (F1 F2)+ = F+ Otherwise, checking updates for violation of functional dependencies is expensive. Database Systems Concepts 7.7 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · R = (A, B, C) F = {A B, B C} · R1 = (A, B), R2 = (B, C) ­ Lossless-join decomposition: R1 R2 = {B} and B BC ­ Dependency preserving · R1 = (A, B), R2 = (A, C) ­ Lossless-join decomposition: R1 R2 = {A} and A AB ­ Not dependency preserving (cannot check B C without computing R1 1 R2) Database Systems Concepts 7.8 Silberschatz, Korth and Sudarshan c 1997 ' & $ Boyce-Codd Normal Form 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: · is trivial (i.e., ) · is a superkey for R Database Systems Concepts 7.9 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · R = (A, B, C) F = {A B B C} Key = {A} · R is not in BCNF · Decomposition R1 = (A, B), R2 = (B, C) ­ R1 and R2 in BCNF ­ Lossless-join decomposition ­ Dependency preserving Database Systems Concepts 7.10 Silberschatz, Korth and Sudarshan c 1997 ' & $ BCNF Decomposition Algorithm result := {R}; done := false; compute F+ ; while (not done) do if (there is a schema Ri in result that is not in BCNF) then begin let be a nontrivial functional dependency that holds on Ri such that Ri is not in F+ , and = ; result := (result - Ri ) (Ri - ) (, ); end else done := true; Note: each Ri is in BCNF, and decomposition is lossless-join. Database Systems Concepts 7.11 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example of BCNF Decomposition · 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} · Decomposition ­ R1 = (branch-name, branch-city, assets) ­ R2 = (branch-name, customer-name, loan-number, amount) ­ R3 = (branch-name, loan-number, amount) ­ R4 = (customer-name, loan-number) · Final decomposition R1, R3, R4 Database Systems Concepts 7.12 Silberschatz, Korth and Sudarshan c 1997 ' & $ BCNF and Dependency Preservation It is not always possible to get a BCNF decomposition that is dependency preserving · R = (J, K, L) F = {JK L L K} Two candidate keys = JK and JL · R is not in BCNF · Any decomposition of R will fail to preserve JK L Database Systems Concepts 7.13 Silberschatz, Korth and Sudarshan c 1997 ' & $ Third Normal Form · 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 a superkey for R ­ Each attribute A in - is contained in a candidate key for R. · If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). Database Systems Concepts 7.14 Silberschatz, Korth and Sudarshan c 1997 ' & $ 3NF (Cont.) · Example ­ R = (J, K, L) F = {JK L, L K} ­ Two candidate keys: JK and JL ­ R is in 3NF JK L JK is a superkey L K K is contained in a candidate key · Algorithm to decompose a relation schema R into a set of relation schemas {R1, R2, ..., Rn} such that: ­ each relation schema Ri is in 3NF ­ lossless-join decomposition ­ dependency preserving Database Systems Concepts 7.15 Silberschatz, Korth and Sudarshan c 1997 ' & $ 3NF Decomposition Algorithm Let Fc be a canonical cover for F; i := 0; for each functional dependency in Fc do if none of the schemas Rj , 1 j i contains then begin i := i + 1; Ri := ; end if none of the schemas Rj , 1 j i contains a candidate key for R then begin i := i + 1; Ri := any candidate key for R; end return (R1, R2, ..., Ri) Database Systems Concepts 7.16 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · 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 key is: {customer-name, branch-name} Database Systems Concepts 7.17 Silberschatz, Korth and Sudarshan c 1997 ' & $ Applying 3NF to Banker - info - schema · 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 with the decomposition process. Database Systems Concepts 7.18 Silberschatz, Korth and Sudarshan c 1997 ' & $ Comparison of BCNF and 3NF · It is always possible to decompose a relation into relations in 3NF and ­ the decomposition is lossless ­ dependencies are preserved · It is always possible to decompose a relation into relations in BCNF and ­ the decomposition is lossless ­ it may not be possible to preserve dependencies Database Systems Concepts 7.19 Silberschatz, Korth and Sudarshan c 1997 ' & $ Comparison of BCNF and 3NF (Cont.) · R = (J, K, L) F = {JK L L K} · Consider the following relation J L K j1 l1 k1 j2 l1 k1 j3 l1 k1 null l2 k2 · A schema that is in 3NF but not in BCNF has the problems of ­ repetition of information (e.g., the relationship l1, k1) ­ need to use null values (e.g., to represent the relationship l2, k2 where there is no corresponding value for J). Database Systems Concepts 7.20 Silberschatz, Korth and Sudarshan c 1997 ' & $ Design Goals · Goal for a relational database design is: ­ BCNF. ­ Lossless join. ­ Dependency preservation. · If we cannot achieve this, we accept: ­ 3NF. ­ Lossless join. ­ Dependency preservation. Database Systems Concepts 7.21 Silberschatz, Korth and Sudarshan c 1997 ' & $ Normalization Using Multivalued Dependencies · There are database schemas in BCNF that do not seem to be sufficiently normalized · Consider a database classes(course, teacher, book) such that (c,t,b) classes means that t is qualified to teach c, and b is a required textbook for c · The database is supposed to list for each course the set of teachers any one of which can be the course's instructor, and the set of books, all of which are required for the course (no matter who teaches it). Database Systems Concepts 7.22 Silberschatz, Korth and Sudarshan c 1997 ' & $ course teacher book database Avi Korth database Avi Ullman database Hank Korth database Hank Ullman database Sudarshan Korth database Sudarshan Ullman operating systems Avi Silberschatz operating systems Avi Shaw operating systems Jim Silberschatz operating systems Jim Shaw classes · Since there are no non-trivial dependencies, (course, teacher, book) is the only key, and therefore the relation is in BCNF · Insertion anomalies ­ i.e., if Sara is a new teacher that can teach database, two tuples need to be inserted (database, Sara, Korth) (database, Sara, Ullman) Database Systems Concepts 7.23 Silberschatz, Korth and Sudarshan c 1997 ' & $ · Therefore, it is better to decompose classes into: course teacher database Avi database Hank database Sudarshan operating systems Avi operating systems Jim teaches course book database Korth database Ullman operating systems Silberschatz operating systems Shaw text · We shall see that these two relations are in Fourth Normal Form (4NF) Database Systems Concepts 7.24 Silberschatz, Korth and Sudarshan c 1997 ' & $ Multivalued Dependencies (MVDs) · Let R be a relation schema and let R and R. The multivalued dependency holds on R if in any legal relation r(R), for all pairs of tuples t1 and t2 in r such that t1[] = t2[], there exist tuples t3 and t4 in r such that: t1[] = t2[] = t3[] = t4[] t3[] = t1[] t3[R - ] = t2[R - ] t4[] = t2[] t4[R - ] = t1[R - ] Database Systems Concepts 7.25 Silberschatz, Korth and Sudarshan c 1997 ' & $ MVD (Cont.) · Tabular representation of R - - t1 a1 ... ai ai + 1 ... aj aj + 1 ... an t2 a1 ... ai bi + 1 ... bj bj + 1 ... bn t3 a1 ... ai ai + 1 ... aj bj + 1 ... bn t4 a1 ... ai bi + 1 ... bj aj + 1 ... an Database Systems Concepts 7.26 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · Let R be a relation schema with a set of attributes that are partitioned into 3 nonempty subsets, Y, Z, W · We say that Y Z (Y multidetermines Z) if and only if for all possible relations r(R) < y1, z1, w1 > r and < y1, z2, w2 > r then < y1, z1, w2 > r and < y1, z2, w1 > r · Note that since the behavior of Z and W are identical it follows that Y Z iff Y W Database Systems Concepts 7.27 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example (Cont.) · In our example: course teacher course book · The above formal definition is supposed to formalize the notion that given a particular value of Y (course) it has associated with it a set of values of Z (teacher) and a set of values of W (book), and these two sets are in some sense independent of each other. · Note: ­ If Y Z then Y Z ­ Indeed we have (in above notation) Z1 = Z2 The claim follows. Database Systems Concepts 7.28 Silberschatz, Korth and Sudarshan c 1997 ' & $ Use of Multivalued Dependencies · We use multivalued dependencies in two ways: 1. To test relations to determine whether they are legal under a given set of functional and multivalued dependencies. 2. To specify constraints on the set of legal relations. We shall thus concern ourselves only with relations that satisfy a given set of functional and multivalued dependencies. · If a relation r fails to satisfy a given multivalued dependency, we can construct a relation r that does satisfy the multivalued dependency by adding tuples to r. Database Systems Concepts 7.29 Silberschatz, Korth and Sudarshan c 1997 ' & $ Theory of Multivalued Dependencies · Let D denote a set of functional and multivalued dependencies. The closure D+ of D is the set of all functional and multivalued dependencies logically implied by D. · Sound and complete inference rules for functional and multivalued dependencies: 1. Reflexivity rule. If is a set of attributes and , then holds. 2. Augmentation rule. If holds and is a set of attributes, then holds. 3. Transitivity rule. If holds and holds, then holds. Database Systems Concepts 7.30 Silberschatz, Korth and Sudarshan c 1997 ' & $ Theory of Multivalued Dependencies (Cont.) 4. Complementation rule. If holds, then R - - holds. 5. Multivalued augmentation rule. If holds and R and , then holds. 6. Multivalued transitivity rule. If holds and holds, then - holds. 7. Replication rule. If holds, then . 8. Coalescence rule. If holds and and there is a such that R and = and , then holds. Database Systems Concepts 7.31 Silberschatz, Korth and Sudarshan c 1997 ' & $ Simplification of the Computation of D+ · We can simplify the computation of the closure of D by using the following rules (proved using rules 1­8). ­ Multivalued union rule. If holds and holds, then holds. ­ Intersection rule. If holds and holds, then holds. ­ Difference rule. If holds and holds, then - holds and - holds. Database Systems Concepts 7.32 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · R = (A, B, C, G, H, I) D = {A B B HI CG H} · Some members of D+ : ­ A CGHI. Since A B, the complementation rule (4) implies that A R - B - A. Since R - B - A = CGHI, so A CGHI. ­ A HI. Since A B and B HI, the multivalued transitivity rule (6) implies that A HI - B. Since HI - B = HI, A HI. Database Systems Concepts 7.33 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example (Cont.) · Some members of D+ (cont.): ­ B H. Apply the coalescence rule (8); B HI holds. Since H HI and CG H and CG HI = , the coalescence rule is satisfied with being B, being HI, being CG, and being H. We conclude that B H. ­ A CG. A CGHI and A HI. By the difference rule, A CGHI - HI. Since CGHI - HI = CG, A CG. Database Systems Concepts 7.34 Silberschatz, Korth and Sudarshan c 1997 ' & $ Fourth Normal Form · A relation schema R is in 4NF with respect to a set D of functional and multivalued dependencies if for all multivalued dependencies in D+ of the form , where R and R, at least one of the following hold: ­ is trivial (i.e., or = R) ­ is a superkey for schema R · If a relation is in 4NF it is in BCNF Database Systems Concepts 7.35 Silberschatz, Korth and Sudarshan c 1997 ' & $ 4NF Decomposition Algorithm result := {R}; done := false; compute F+ ; while (not done) do if (there is a schema Ri in result that is not in 4NF) then begin let be a nontrivial multivalued dependency that holds on Ri such that Ri is not in F+ , and = ; result := (result - Ri ) (Ri - ) (, ); end else done := true; Note: each Ri is in 4NF, and decomposition is lossless-join. Database Systems Concepts 7.36 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · R = (A, B, C, G, H, I) F = {A B B HI CG H} · R is not in 4NF since A B and A is not a superkey for R · Decomposition a) R1 = (A, B) (R1 is in 4NF) b) R2 = (A, C, G, H, I) (R2 is not in 4NF) c) R3 = (C, G, H) (R3 is in 4NF) d) R4 = (A, C, G, I) (R4 is not in 4NF) · Since A B and B HI, A HI, A I e) R5 = (A, I) (R5 is in 4NF) f) R6 = (A, C, G) (R6 is in 4NF) Database Systems Concepts 7.37 Silberschatz, Korth and Sudarshan c 1997 ' & $ Multivalued Dependency Preservation · Let R1, R2, . . . , Rn be a decomposition of R, and D a set of both functional and multivalued dependencies. · The restriction of D to Ri is the set Di , consisting of ­ All functional dependencies in D+ that include only attributes of Ri ­ All multivalued dependencies of the form Ri where Ri and is in D+ · The decomposition is dependency-preserving with respect to D if, for every set of relations r1(R1), r2(R2), . . ., rn(Rn) such that for all i, ri satisfies Di , there exists a relation r(R) that satisfies D and for which ri = Ri (r) for all i. · Decomposition into 4NF may not be dependency preserving (even on just the multivalued dependencies) Database Systems Concepts 7.38 Silberschatz, Korth and Sudarshan c 1997 ' & $ Normalization Using Join Dependencies · Join dependencies constrain the set of legal relations over a schema R to those relations for which a given decomposition is a lossless-join decomposition. · Let R be a relation schema and R1, R2, ..., Rn be a decomposition of R. If R = R1 R2 ... Rn, we say that a relation r(R) satisfies the join dependency *(R1, R2, ..., Rn) if: r = R1 (r) 1 R2 (r) 1 ... 1 Rn (r) A join dependency is trivial if one of the Ri is R itself. · A join dependency *(R1, R2) is equivalent to the multivalued dependency R1 R2 R2. Conversely, is equivalent to ( (R - ), ) · However, there are join dependencies that are not equivalent to any multivalued dependency. Database Systems Concepts 7.39 Silberschatz, Korth and Sudarshan c 1997 ' & $ Project-Join Normal Form (PJNF) · A relation schema R is in PJNF with respect to a set D of functional, multivalued, and join dependencies if for all join dependencies in D+ of the form *(R1, R2, ..., Rn) where each Ri R and R = R1 R2 ... Rn, at least one of the following holds: ­ *(R1, R2, ..., Rn) is a trivial join dependency. ­ Every Ri is a superkey for R. · Since every multivalued dependency is also a join dependency, every PJNF schema is also in 4NF. Database Systems Concepts 7.40 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · Consider Loan-info-schema = (branch-name, customer-name, loan-number, amount). · Each loan has one or more customers, is in one or more branches and has a loan amount; these relationships are independent, hence we have the join dependency *((loan-number, branch-name), (loan-number, customer-name), (loan-number, amount)) · Loan-info-schema is not in PJNF with respect to the set of dependencies containing the above join dependency. To put Loan-info-schema into PJNF, we must decompose it into the three schemas specified by the join dependency: ­ (loan-number, branch-name) ­ (loan-number, customer-name) ­ (loan-number, amount) Database Systems Concepts 7.41 Silberschatz, Korth and Sudarshan c 1997 ' & $ Domain-Key Normal Form (DKNY) · Domain declaration. Let A be an attribute, and let dom be a set of values. The domain declaration A dom requires that the A value of all tuples be values in dom. · Key declaration. Let R be a relation schema with K R. The key declaration key (K) requires that K be a superkey for schema R (K R). All key declarations are functional dependencies but not all functional dependencies are key declarations. · General constraint. A general constraint is a predicate on the set of all relations on a given schema. · Let D be a set of domain constraints and let K be a set of key constraints for a relation schema R. Let G denote the general constraints for R. Schema R is in DKNF if D K logically imply G. Database Systems Concepts 7.42 Silberschatz, Korth and Sudarshan c 1997 ' & $ Example · Accounts whose account-number begins with the digit 9 are special high-interest accounts with a minimum balance of $2500. · General constraint: "If the first digit of t[account-number] is 9, then t[balance] 2500." · DKNF design: Regular-acct-schema = (branch-name, account-number, balance) Special-acct-schema = (branch-name, account-number, balance) · Domain constraints for Special-acct-schema require that for each account: ­ The account number begins with 9. ­ The balance is greater than 2500. Database Systems Concepts 7.43 Silberschatz, Korth and Sudarshan c 1997 ' & $ DKNF rephrasing of PJNF Definition · Let R = (A1, A2, ..., An) be a relation schema. Let dom(Ai ) denote the domain of attribute Ai, and let all these domains be infinite. Then all domain constraints D are of the form Ai dom(Ai ). · Let the general constraints be a set G of functional, multivalued, or join dependencies. If F is the set of functional dependencies in G, let the set K of key constraints be those nontrivial functional dependencies in F+ of the form R. · Schema R is in PJNF if and only if it is in DKNF with respect to D, K, and G. Database Systems Concepts 7.44 Silberschatz, Korth and Sudarshan c 1997 ' & $ Alternative Approaches to Database Design · Dangling tuples --Tuples that "disappear" in computing a join. ­ Let r1(R1), r2(R2), ..., rn(Rn) be a set of relations. ­ A tuple t of relation ri is a dangling tuple if t is not in the relation: Ri (r1 1 r2 1 ... 1 rn) · The relation r1 1 r2 1 ... 1 rn is called a universal relation since it involves all the attributes in the "universe" defined by R1 R2 ... Rn. · If dangling tuples are allowed in the database, instead of decomposing a universal relation, we may prefer to synthesize a collection of normal form schemas from a given set of attributes. Database Systems Concepts 7.45 Silberschatz, Korth and Sudarshan c 1997