Chapter 7 Relational Database Design n Pitfalls in
Chapter 7: Relational Database Design n Pitfalls in Relational Database Design n Decomposition n Normalization Using Functional Dependencies n Normalization Using Multivalued Dependencies n Normalization Using Join Dependencies n Domain-Key Normal Form n Alternative Approaches to Database Design Database System Concepts 7. 1 ©Silberschatz, Korth and Sudarshan
Pitfalls in Relational Database Design n Relational database design requies that we find a “good” collection of relation schemas. A bad design may lead to H Repetition of Information. H Inability to represent certain information. n Design Goals: H Avoid redundant data H Ensure that relationships among attributes are represented H Facilitate the checking of updates for violation of database integrity constraints. Database System Concepts 7. 2 ©Silberschatz, Korth and Sudarshan
Example n Consider the relation schema: Lending-schema = (branch-name, branch-city, assets, customer-name, loan-number, amount) n Redundancy: H Data for branch-name, branch-city, assets are repeated for each loan that a branch makes H Wastes space and complicates updating n Null values H Cannot store information about a branch if no loans exist H Can use null values, but they are difficult to handle. Database System Concepts 7. 3 ©Silberschatz, Korth and Sudarshan
Decomposition n 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 n All attributes of an original schema (R) must appear in the decomposition (R 1, R 2): R - R 1 R 2 n Lossless-join decomposition. For all possible relations r on schema R r = R 1 (r) |x| R 2 (r) Database System Concepts 7. 4 ©Silberschatz, Korth and Sudarshan
Example of Non Lossless-Join Decomposition of R = (A, B) R 1 = (A)R 2 = (B) A B 1 2 1 1 2 A(r) B(r) r A (r) |x| B (r) Database System Concepts A B 1 2 7. 5 ©Silberschatz, Korth and Sudarshan
Goal — Devise a Theory for the Following n Decide whether a particular relation R is in “good” form. n In the case that a relation R is not in “good” form, decompose it into a set of relations {R 1, R 2, . . . , Rn} such that H each relation is in good form H the decomposition is a lossless-join decomposition n Our theory is based on: H functional dependencies H multivalued dependencies Database System Concepts 7. 6 ©Silberschatz, Korth and Sudarshan
Normalization Using Functional Dependencies When we decompose a relation schema R with a set of functional dependencies F into R 1 and R 2 we want: n Lossless-join decomposition: At least one of the following dependencies is in F+: H R 1 R 2 R 1 H R 1 R 2 n No redundancy: The relations R 1 and R 2 preferably should be in either Boyce-Godd Normal Form or Third Normal Form. n Dependency preservation: Let Fi be the set of dependencies F+ that include only attributes in Ri. Test to see if: H (F 1 F 2) = F+ Otherwise, checking updates for violation of functional dependencies is expensive. Database System Concepts 7. 7 ©Silberschatz, Korth and Sudarshan
Example n R = (A, B, C) F = {A B, B C) n R 1 = (A, B), R 2 = (B, C) H Lossless-join decomposition: R 1 R 2 = {B} and B BC H Dependency preserving n R 1 = (A, B), R 2 = (A, C) H Lossless-join decomposition: R 1 R 2 = {A} and A AB H Not dependency preserving (cannot check B C without computing R 1 Database System Concepts 7. 8 R 2 ) ©Silberschatz, Korth and Sudarshan
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: n is trivial (i. e. , ) n is a superkey for R Database System Concepts 7. 9 ©Silberschatz, Korth and Sudarshan
Example n R = (A, B, C) F = {A B B C} Key = {A} n R is not in BCNF n Decomposition R 1 = (A, B), R 2 = (B, C) H R 1 and R 2 in BCNF H Lossless-join decomposition H Dependency preserving Database System Concepts 7. 10 ©Silberschatz, Korth and Sudarshan
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; Database System Concepts 7. 11 ©Silberschatz, Korth and Sudarshan
Example of BCNF Decomposition n 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} n Decomposition H R 1 = (branch-name, branch-city, assets) H R 2 = (branch-name, customer-name, loan-number, amount) H R 3 = (branch-name, loan-number, amount) H R 4 = (customer-name, loan-number) n Final decomposition R 1 , R 3 , R 4 Database System Concepts 7. 12 ©Silberschatz, Korth and Sudarshan
BCNF and Dependency Preservation It is not always possible to get a BCNF decomposition that is dependency preserving n R = (J, K, L) F = {JK L L K} Two candidate keys = JK and JL n R is not in BCNF n Any decomposition of R will fail to preserve JK L Database System Concepts 7. 13 ©Silberschatz, Korth and Sudarshan
Third Normal Form n A relation schema R is in third normal form (3 NF) if for all: in F+ at least one of the following holds: H is trivial (i. e. , ) H is a superkey for R H Each attribute A in – is contained in a candidate key for R. n If a relation is in BCNF it is in 3 NF (since in BCNF one of the first two conditions above must hold). Database System Concepts 7. 14 ©Silberschatz, Korth and Sudarshan
3 NF (Cont. ) n Example H R = (J, K, L) F = {JK L, L K} H Two candidate keys: JK and JL H R is in 3 NF JK L JK is a superkey L K K is contained in a candidate key n Algorithm to decompose a relation schema R into a set of relation schemas {R 1, R 2, . . . , Rn} such that: H each relation schema Ri is in 3 NF H lossless-join decomposition H dependency preserving Database System Concepts 7. 15 ©Silberschatz, Korth and Sudarshan
3 NF 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 1 j i contains a candidate key for R then begin i : = i + 1; Ri : = any candidate key for R; end return (R 1, R 2, . . . , Ri) Database System Concepts 7. 16 ©Silberschatz, Korth and Sudarshan
Example n Relation schema: Banker-info-schema = (branch-name, customer-name, banker-name, office-number) n The functional dependencies for this relation schema are: banker-name branch-name office-number customer-name branch-name banker-name n The key is: {customer-name, branch-name} Database System Concepts 7. 17 ©Silberschatz, Korth and Sudarshan
Applying 3 NF to Banker — info —schema n The for loop in the algorithm causes us to include the following schemas in our decomposition: Banker-office-schema = (banker-name, branchname, office-number) Banker-schema = (customer-name, branch-name, banker-name) n Since Banker-schema contains a candidate key for Banker-info-schema, we are done with the decomposition process. Database System Concepts 7. 18 ©Silberschatz, Korth and Sudarshan
Comparison of BCNF and 3 NF n It is always possible to decompose a relation into relations in 3 NF and H the decomposition is lossless H the dependencies are preserved n It is always possible to decompose a relation into relations in BCNF and H the decomposition is lossless H it may not be possible to preserve dependencies. Database System Concepts 7. 19 ©Silberschatz, Korth and Sudarshan
Comparison of BCNF and 3 NF (Cont. ) n R = (J, K, L) F = {JK L L K} n Consider the following relation J L K j 1 l 1 k 1 j 2 l 1 k 1 j 3 l 1 k 1 null l 2 k 2 A schema that is in 3 NF 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). Database System Concepts 7. 20 ©Silberschatz, Korth and Sudarshan
Design Goals n Goal for a relational database design is: H BCNF. H Lossless join. H Dependency preservation. n If we cannot achieve this, we accept: H 3 NF H Lossless join. H Dependency preservation. Database System Concepts 7. 21 ©Silberschatz, Korth and Sudarshan
Normalization Using Multivalued Dependencies n There are database schemas in BCNF that do not seem to be sufficiently normalized n 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 n 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 System Concepts 7. 22 ©Silberschatz, Korth and Sudarshan
course database database operating systems teacher Avi Hank Sudarshan Avi Jim book Korth Ullman Silberschatz Shaw n Since there are non-trivial dependencies, (course, teacher, book) is the only key, and therefore the relation is in BCNF n 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 System Concepts 7. 23 ©Silberschatz, Korth and Sudarshan
n Therefore, it is better to decompose classes into: course teacher database Avi database Hank database Sudarshan operating systems Avi operating systems Jim teaches course teacher database operating systems Korth Ullman Silberschatz Shaw text We shall see that these two relations are in Fourth Normal Form (4 NF) Database System Concepts 7. 24 ©Silberschatz, Korth and Sudarshan
Multivalued Dependencies (MVDs) n 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 for tuples t 1 and t 2 in r such that t 1[ ] = t 2 [ ], there exist tuples t 3 and t 4 in r such that: t 1[ ] = t 2 [ ] = t 3 [ ] t 4 [ ] t 3 [ ] = t 1 [ ] t 3[R – ] = t 2[R – ] t 4 ] = t 2 [ ] t 4[R – ] = t 1[R – ] Database System Concepts 7. 25 ©Silberschatz, Korth and Sudarshan
MVD (Cont. ) n Tabular representation of Database System Concepts R– – t 1 a 1. . . ai ai + 1. . . aj ai + 1. . . an t 2 a 1. . . ai bi + 1. . . bj bj + 1. . . bn t 3 a 1. . . ai ai + 1. . . aj ai + 1. . . an t 4 a 1. . . ai bi + 1. . . bj bj + 1. . . bn 7. 26 ©Silberschatz, Korth and Sudarshan
Example n Let R be a relation schema with a set of attributes that are partitioned into 3 nonempty subsets. Y, Z, W n We say that Y Z (Y multidetermines Z) if and only if for all possible relations r(R) < y 1, z 1, w 1 > r and < y 2, z 2, w 2 > r then < y 1, z 1, w 2 > r and < y 1, z 2, w 1 > r n Note that since the behavior of Z and W are identical it follows that Y Z if Y W Database System Concepts 7. 27 ©Silberschatz, Korth and Sudarshan
Example (Cont. ) n In our example: course teacher course book n 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. n Note: H If Y Z then Y Z H Indeed we have (in above notation) Z 1 = Z 2 The claim follows. Database System Concepts 7. 28 ©Silberschatz, Korth and Sudarshan
Use of Multivalued Dependencies n 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. n If a relation r fails to satisfy a given multivalued dependency, we can construct a relations r that does satisfy the multivalued dependency by adding tuples to r. Database System Concepts 7. 29 ©Silberschatz, Korth and Sudarshan
Theory of Multivalued Dependencies n 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. n 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 If holds and holds, then holds. Database System Concepts 7. 30 ©Silberschatz, Korth and Sudarshan
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 there is a such that R and = and , then holds. Database System Concepts 7. 31 ©Silberschatz, Korth and Sudarshan
Simplification of the Computation of D+ n We can simplify the computation of the closure of D by using the following rules (proved using rules 1 -8). H Multivalued union rule. If If holds and holds, then holds. H Intersection rule. If holds and holds, then holds. H Difference rule. If If holds and holds, then – holds and – holds. Database System Concepts 7. 32 ©Silberschatz, Korth and Sudarshan
Example n R = (A, B, C, G, H, I) D = {A B B HI CG H} n Some members of D+: H A CGHI. Since A B, the complementation rule (4) implies that A R – B – A. Since R – B – A = CGHI, so A CGHI. H A HI. Since A B and B HI, the multivalued transitivity rule (6) implies that B HI – B. Since HI – B = HI, A HI. Database System Concepts 7. 33 ©Silberschatz, Korth and Sudarshan
Example (Cont. ) n Some members of D+ (cont. ): H B H. Apply the coalescence rule (8); B HI holds. Since H HI and CG HI = Ø, the coalescence rule is satisfied with being B, being HI, being CG, and being H. We conclude that B H. H A CGHI and A HI. By the difference rule, A CGHI – HI. Since CGHI – HI = CG, A CG. Database System Concepts 7. 34 ©Silberschatz, Korth and Sudarshan
Fourth Normal Form n A relation schema R is in 4 NF 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: H is trivial (i. e. , or = R) H is a superkey for schema R n If a relation is in 4 NF it is in BCNF Database System Concepts 7. 35 ©Silberschatz, Korth and Sudarshan
Multivalued Dependency Preservation n Let R 1, R 2, . . . , Rn be a decomposition of R 1 and D a set of both functional and multivalued dependencies. n The restriction of D to Ri is the set Di, consisting of H All functional dependencies in D+ that include only attributes of Ri H All multivalued dependencies of the form Ri where Ri and is in D+ n The decomposition is dependency-preserving with respect to D if, for every set of relations r 1(R 1), r 2(R 2), . . . , 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. n Decomposition into 4 NF may not be dependency preserving (even on just the multivalued dependencies) Database System Concepts 7. 36 ©Silberschatz, Korth and Sudarshan
Normalization Using Join Dependencies n Join Dependencies constrain the set of legal relations over a schema R to those relations for which a given decomposition is a lossless decomposition. n Let R be a relation schema and R 1, R 2, . . . , Rn be a decomposition of R. If R = R 1 R 2 . . . Rn, we say that a relation r. R) satisfies the join dependency *(R 1, R 2, . . . , Rn) if: r = R 1(r) |x| R 2 (r) |x|. . . |x| Ri (r) A join dependency is trivial if one of the Ri is R itself. n A join dependency *(R 1, R 2) is equivalent to the multivalued dependency R 1 R 2. Conversely, is equivalent to *( (R – ), ) n However, there are join dependencies that are not equivalent to any multivalued dependency. Database System Concepts 7. 37 ©Silberschatz, Korth and Sudarshan
Project-Join Normal Form (PJNF) n 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 *(R 1, R 2, . . . , Rn) where each Ri R and R = R 1 R 2 . . . Rn 1 at least one of the following holds: H *(R 1, R 2, . . . , Rn) is a trivial join dependency. H Every Ri is a superkey for R. n Since every multivalued dependency is also a join dependency, every PJNF schema is also in 4 NF. Database System Concepts 7. 38 ©Silberschatz, Korth and Sudarshan
Example n Consider Loan-info-schema = (branch-name, customer- name, loan-number, amount). n 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, customername), (loan-number, amount)) n 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 inot the three schemas specified by the join dependency: H (loan-number, branch-name) H (loan-number, customer-name) H (loan-number, amount) Database System Concepts 7. 39 ©Silberschatz, Korth and Sudarshan
Domain-Key Normal Form (DKNY) n 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. n 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. n General constraint. A general constraint is a predicate on the set of all relations on a given schema. n 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 System Concepts 7. 40 ©Silberschatz, Korth and Sudarshan
Example n Accounts whose account-number begins with the digit 9 are special high-interest accounts with a minimum balance of $2500. n General constraint: “If the first digit of I[account-number] is 9, then I[balance] 2500. * n DKNF design: Regular-acct-schema = (branch-name, account-number, balance) Special-acct-schema = (branch-name, account-number, balance) n Domain constraints for Special-acct-schema require that for each account: H The account number begins with 9. H The balance is greater than 2500. Database System Concepts 7. 41 ©Silberschatz, Korth and Sudarshan
DKNF rephrasing of PJNF Definition n Let R = (A 1, A 2, . . . , An) be a relation schema. Let dom(Ai) denote the domain of attribute Ai, and let all these domains be infinite. The all domain constraints D are of the form Ai dom(Ai). n 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. n Schema R is in PJNF if and only if it is in DKNF with respect to D, K, and G. Database System Concepts 7. 42 ©Silberschatz, Korth and Sudarshan
Alternative Approaches to Database Design n Dangling tuples — Tuples that “disappear” in computing a join. H Let r 1(R 1), r 2(R 2), . . . , rn(Rn), be a set of relations. H A tuple t of relation ri is a dangling tuple if t is not in the relation: Ri (r 1 |x| r 2 |x|. . . |x| rn) H The relation r 1 |x| r 2 |x|. . . |x| rn is called a universal relation since it involves all the attributes in the “universe” defined by R 1 . . . Rn. H 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 System Concepts 7. 43 ©Silberschatz, Korth and Sudarshan
- Slides: 43