Chapter 7 Relational Database Design Chapter 7 Relational

Chapter 7: Relational Database Design

Chapter 7: Relational Database Design n First Normal Form n Pitfalls in Relational Database Design n Functional Dependencies n Decomposition n Boyce-Codd Normal Form n Third Normal Form n Multivalued Dependencies and Fourth Normal Form n Overall Database Design Process Database System Concepts 7. 2 ©Silberschatz, Korth and Sudarshan

Decomposition n Decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city, assets) Loan-info-schema = (customer-name, loan-number, branch-name, 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) R 2 (r) n A decomposition of R into R 1 and R 2 is lossless join if (not: if and only if) at least one of the following dependencies is in F+: H R 1 R 2 R 1 H R 1 R 2 Database System Concepts 7. 3 ©Silberschatz, Korth and Sudarshan

Normalization Using Functional Dependencies n When we decompose a relation schema R with a set of functional dependencies F into R 1, R 2, . . , Rn we want H Lossless-join decomposition: Otherwise decomposition would result in information loss. H Dependency preservation: Let Fi be the set of dependencies F+ that include only attributes in Ri. 4 Preferably the decomposition should be dependency preserving, that is, (F 1 F 2 … Fn)+ = F+ 4 Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive. H No redundancy: The relations Ri preferably should be in either Boyce. Codd Normal Form or Third Normal Form. Database System Concepts 7. 4 ©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 In brief: R is in BCNF if all nontrivial dependencies are based on superkeys Database System Concepts 7. 5 ©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. 6 ©Silberschatz, Korth and Sudarshan

Testing for BCNF n 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. n 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+. H 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. n However, using only F is incorrect when testing a relation in a decomposition of R H E. g. Consider R (A, B, C, D), with F = { A B, B C} 4 Decompose R into R 1(A, B) and R 2(A, C, D) 4 Neither of the dependencies in F contain only attributes from (A, C, D) so we might be mislead into thinking R 2 satisfies BCNF. 4 In fact, dependency A C in F+ shows R 2 is not in BCNF. Database System Concepts 7. 7 ©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; Note: each Ri is in BCNF, and decomposition is lossless-join. Database System Concepts 7. 8 ©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. 9 ©Silberschatz, Korth and Sudarshan

Testing Decomposition for BCNF n To check if a relation Ri in a decomposition of R is in BCNF, H Either test Ri for BCNF with respect to the restriction of F to Ri (that is, all FDs in F+ that contain only attributes from Ri) H or use the original set of dependencies F that hold on R, but with the following test: – for every set of attributes Ri, check that + (the attribute closure of ) either includes no attribute of Ri- , or includes all attributes of Ri. 4 If the condition is violated by some in F, the dependency ( + - ) Ri can be shown to hold on Ri, and Ri violates BCNF. 4 We use above dependency to decompose Ri Database System Concepts 7. 10 ©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. 11 ©Silberschatz, Korth and Sudarshan

Third Normal Form: Motivation n There are some situations where H BCNF is not dependency preserving, and H efficient checking for FD violation on updates is important n Solution: define a weaker normal form, called Third Normal Form. H Allows some redundancy (with resultant problems; we will see examples later) H But FDs can be checked on individual relations without computing a join. H There is always a lossless-join, dependency-preserving decomposition into 3 NF. Database System Concepts 7. 12 ©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. (NOTE: each attribute may be in a different candidate key) n If a relation is in BCNF it is in 3 NF (since in BCNF one of the first two conditions above must hold). n Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later). Database System Concepts 7. 13 ©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 L K JK is a superkey K is contained in a candidate key n BCNF decomposition has (JL) and (LK) n Testing for JK L requires a join n There is some redundancy in this schema n Equivalent to example in book: Banker-schema = (branch-name, customer-name, banker-name) banker-name branch name customer-name banker-name Database System Concepts 7. 14 ©Silberschatz, Korth and Sudarshan

Testing for 3 NF n Optimization: Need to check only FDs in F, need not check all FDs in F+. n Use attribute closure to check for each dependency , if is a superkey. n If is not a superkey, we have to verify whether each attribute in is contained in a candidate key of R H this test is rather more expensive, since it involve finding candidate keys H testing for 3 NF has been shown to be NP-hard H Interestingly, decomposition into third normal form (described shortly) can be done in polynomial time 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 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

3 NF Decomposition Algorithm (Cont. ) n Above algorithm ensures: H each relation schema Ri is in 3 NF H decomposition is dependency preserving and lossless-join H Proof of correctness is at end of this file (click here) Database System Concepts 7. 17 ©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. 18 ©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, branch-name, 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. 19 ©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. 20 ©Silberschatz, Korth and Sudarshan

Comparison of BCNF and 3 NF (Cont. ) n Example of problems due to redundancy in 3 NF H R = (J, K, L) F = {JK L, L K} 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 n repetition of information (e. g. , the relationship l 1, k 1) n 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. 21 ©Silberschatz, Korth and Sudarshan

Design Goals n Goal for a relational database design is: H Lack of redundancy. H Lossless join. H Dependency preservation. n If we cannot achieve this, we accept one of H Lack of dependency preservation H Redundancy due to use of 3 NF n Interestingly, SQL does not provide a direct way of specifying functional dependencies other than superkeys. Can specify FDs using assertions, but they are expensive to test n Even if we had a dependency preserving decomposition, using SQL we would not be able to efficiently test a functional dependency whose left hand side is not a key. Database System Concepts 7. 22 ©Silberschatz, Korth and Sudarshan

Testing for FDs Across Relations n If decomposition is not dependency preserving, we can have an extra materialized view for each dependency in Fc that is not preserved in the decomposition n The materialized view is defined as a projection on of the join of the relations in the decomposition n Many newer database systems support materialized views and database system maintains the view when the relations are updated. H No extra coding effort for programmer. n The functional dependency is expressed by declaring as a candidate key on the materialized view. n Checking for candidate key cheaper than checking n BUT: H Space overhead: for storing the materialized view H Time overhead: Need to keep materialized view up to date when relations are updated H Database system may not support key declarations on materialized views Database System Concepts 7. 23 ©Silberschatz, Korth and Sudarshan

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. 24 ©Silberschatz, Korth and Sudarshan

Multivalued Dependencies (Cont. ) course database database operating systems teacher Avi Hank Sudarshan Avi Jim book DB Concepts Ullman OS Concepts Shaw classes n There are no non-trivial functional dependencies 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, DB Concepts) (database, Sara, Ullman) Database System Concepts 7. 25 ©Silberschatz, Korth and Sudarshan

Multivalued Dependencies (Cont. ) n Therefore, it is better to decompose classes into: course teacher database operating systems Avi Hank Sudarshan Avi Jim teaches course book database operating systems DB Concepts Ullman OS Concepts Shaw text We shall see that these two relations are in Fourth Normal Form (4 NF) Database System Concepts 7. 26 ©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. 27 ©Silberschatz, Korth and Sudarshan

MVD (Cont. ) n Tabular representation of Database System Concepts 7. 28 ©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 2, 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. 29 ©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. 30 ©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 relation r that does satisfy the multivalued dependency by adding tuples to r. Database System Concepts 7. 31 ©Silberschatz, Korth and Sudarshan

Theory of MVDs n From the definition of multivalued dependency, we can derive the following rule: H If , then That is, every functional dependency is also a multivalued dependency n The closure D+ of D is the set of all functional and multivalued dependencies logically implied by D. H We can compute D+ from D, using the formal definitions of functional dependencies and multivalued dependencies. H We can manage with such reasoning for very simple multivalued dependencies, which seem to be most common in practice H For complex dependencies, it is better to reason about sets of dependencies using a system of inference rules (see Appendix C). Database System Concepts 7. 32 ©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. 33 ©Silberschatz, Korth and Sudarshan

Restriction of 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+ Database System Concepts 7. 34 ©Silberschatz, Korth and Sudarshan

4 NF Decomposition Algorithm result: = {R}; done : = false; compute D+; Let Di denote the restriction of D+ to Ri while (not done) if (there is a schema Ri in result that is not in 4 NF) then begin let be a nontrivial multivalued dependency that holds on Ri such that Ri is not in Di, and ; result : = (result - Ri) (Ri - ) ( , ); end else done: = true; Note: each Ri is in 4 NF, and decomposition is lossless-join Database System Concepts 7. 35 ©Silberschatz, Korth and Sudarshan

Example n R =(A, B, C, G, H, I) F ={ A B B HI CG H } n R is not in 4 NF since A B and A is not a superkey for R n Decomposition a) R 1 = (A, B) (R 1 is in 4 NF) b) R 2 = (A, C, G, H, I) (R 2 is not in 4 NF) c) R 3 = (C, G, H) (R 3 is in 4 NF) d) R 4 = (A, C, G, I) (R 4 is not in 4 NF) n Since A B and B HI, A I e) R 5 = (A, I) (R 5 is in 4 NF) f)R 6 = (A, C, G) (R 6 is in 4 NF) Database System Concepts 7. 36 ©Silberschatz, Korth and Sudarshan

Further Normal Forms n Join dependencies generalize multivalued dependencies H lead to project-join normal form (PJNF) (also called fifth normal form) n A class of even more general constraints, leads to a normal form called domain-key normal form. n Problem with these generalized constraints: are hard to reason with, and no set of sound and complete set of inference rules exists. n Hence rarely used Database System Concepts 7. 37 ©Silberschatz, Korth and Sudarshan

Overall Database Design Process n We have assumed schema R is given H R could have been generated when converting E-R diagram to a set of tables. H R could have been a single relation containing all attributes that are of interest (called universal relation). H R could have been the result of some ad hoc design of relations, which we then test/convert to normal form. Database System Concepts 7. 38 ©Silberschatz, Korth and Sudarshan

ER Model and Normalization n When an E-R diagram is carefully designed, identifying all entities correctly, the tables generated from the E-R diagram should not need further normalization. n However, in a real (imperfect) design there can be FDs from non-key attributes of an entity to other attributes of the entity n E. g. employee entity with attributes department-number and department-address, and an FD department-number departmentaddress H Good design would have made department an entity n FDs from non-key attributes of a relationship set possible, but rare --- most relationships are binary Database System Concepts 7. 39 ©Silberschatz, Korth and Sudarshan

Universal Relation Approach 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 r of the relation ri is a dangling tuple if r is not in the relation: Ri (r 1 r 2 … rn ) n The relation r 1 r 2 … rn is called a universal relation since it involves all the attributes in the “universe” defined by R 1 R 2 … Rn n 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. 40 ©Silberschatz, Korth and Sudarshan

Universal Relation Approach n Dangling tuples may occur in practical database applications. n They represent incomplete information n E. g. may want to break up information about loans into: (branch-name, loan-number) (loan-number, amount) (loan-number, customer-name) n Universal relation would require null values, and have dangling tuples Database System Concepts 7. 41 ©Silberschatz, Korth and Sudarshan

Universal Relation Approach (Contd. ) n A particular decomposition defines a restricted form of incomplete information that is acceptable in our database. H Above decomposition requires at least one of customer-name, branch-name or amount in order to enter a loan number without using null values H Rules out storing of customer-name, amount without an appropriate loan-number (since it is a key, it can't be null either!) n Universal relation requires unique attribute names unique role assumption H e. g. customer-name, branch-name n Reuse of attribute names is natural in SQL since relation names can be prefixed to disambiguate names Database System Concepts 7. 42 ©Silberschatz, Korth and Sudarshan

Denormalization for Performance n May want to use non-normalized schema for performance n E. g. displaying customer-name along with account-number and balance requires join of account with depositor n Alternative 1: Use denormalized relation containing attributes of account as well as depositor with all above attributes H faster lookup H Extra space and extra execution time for updates H extra coding work for programmer and possibility of error in extra code n Alternative 2: use a materialized view defined as account depositor H Benefits and drawbacks same as above, except no extra coding work for programmer and avoids possible errors Database System Concepts 7. 43 ©Silberschatz, Korth and Sudarshan

Other Design Issues n Some aspects of database design are not caught by normalization n Examples of bad database design, to be avoided: Instead of earnings(company-id, year, amount), use H earnings-2000, earnings-2001, earnings-2002, etc. , all on the schema (company-id, earnings). 4 Above are in BCNF, but make querying across years difficult and needs new table each year H company-year(company-id, earnings-2000, earnings-2001, earnings-2002) 4 Also in BCNF, but also makes querying across years difficult and requires new attribute each year. 4 Is an example of a crosstab, where values for one attribute become column names 4 Used in spreadsheets, and in data analysis tools Database System Concepts 7. 44 ©Silberschatz, Korth and Sudarshan

Proof of Correctness of 3 NF Decomposition Algorithm

Correctness of 3 NF Decomposition Algorithm n 3 NF decomposition algorithm is dependency preserving (since there is a relation for every FD in Fc) n Decomposition is lossless join H A candidate key (C) is in one of the relations Ri in decomposition H Closure of candidate key under Fc must contain all attributes in R. H Follow the steps of attribute closure algorithm to show there is only one tuple in the join result for each tuple in Ri Database System Concepts 7. 46 ©Silberschatz, Korth and Sudarshan

Correctness of 3 NF Decomposition Algorithm (Contd. ) Claim: if a relation Ri is in the decomposition generated by the above algorithm, then Ri satisfies 3 NF. n Let Ri be generated from the dependency n Let B be any non-trivial functional dependency on Ri. (We need only consider FDs whose right-hand side is a single attribute. ) n Now, B can be in either or but not in both. Consider each case separately. Database System Concepts 7. 47 ©Silberschatz, Korth and Sudarshan

Correctness of 3 NF Decomposition (Contd. ) n Case 1: If B in : H If is a superkey, the 2 nd condition of 3 NF is satisfied H Otherwise must contain some attribute not in H Since B is in F+ it must be derivable from Fc, by using attribute closure on . H Attribute closure not have used - if it had been used, must be contained in the attribute closure of , which is not possible, since we assumed is not a superkey. H Now, using ( - {B}) and B, we can derive B (since , and B since B is non-trivial) H Then, B is extraneous in the right-hand side of ; which is not possible since is in Fc. H Thus, if B is in then must be a superkey, and the secondition of 3 NF must be satisfied. Database System Concepts 7. 48 ©Silberschatz, Korth and Sudarshan

Correctness of 3 NF Decomposition (Contd. ) n Case 2: B is in . H Since is a candidate key, the third alternative in the definition of 3 NF is trivially satisfied. H In fact, we cannot show that is a superkey. H This shows exactly why the third alternative is present in the definition of 3 NF. Q. E. D. Database System Concepts 7. 49 ©Silberschatz, Korth and Sudarshan

End of Chapter

An Example of Redundancy in a BCNF Relation Database System Concepts 7. 51 ©Silberschatz, Korth and Sudarshan

An Illegal bc Relation Database System Concepts 7. 52 ©Silberschatz, Korth and Sudarshan
- Slides: 52