CSL 451 Introduction to Database Systems Relational Database Design Department of Computer Science and Engineering Indian Institute of Technology Ropar Narayanan (CK) Chatapuram Krishnan!
Recap - 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 8.2
Testing for BCNF To check if a non-trivial dependency α β causes a violation of BCNF 1. compute α + (the attribute closure of α), and 2. verify that it includes all attributes of R, that is, it is a superkey of R. Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F +. 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. However, simplified test using only F is incorrect when testing a relation in a decomposition of R Consider R = (A, B, C, D, E), with F = { A B, BC D}! Decompose R into R 1 = (A,B) and R 2 = (A,C,D, E)! Neither of the dependencies in F contain only attributes from (A,C,D,E) so we might be mislead into thinking R 2 satisfies BCNF.! In fact, dependency AC D in F + shows R 2 is not in BCNF. 8.3
Testing Decomposition for BCNF To check if a relation R i in a decomposition of R is in BCNF, Either test R i for BCNF with respect to the restriction of F to R i (that is, all FDs in F + that contain only attributes from R i ) or use the original set of dependencies F that hold on R, but with the following test: for every set of attributes α R i, check that α + (the attribute closure of α) either includes no attribute of R i - α, or includes all attributes of R i.! If the condition is violated by some α β in F, the dependency α (α + - α) R i can be shown to hold on R i, and R i violates BCNF.! We use above dependency to decompose R i 8.4
BCNF Decomposition Algorithm result := {R }; done := false; compute F + ; while (not done) do if (there is a schema R i in result that is not in BCNF) then begin let α β be a nontrivial functional dependency that holds on R i such that α R i is not in F +, and α β = ; result := (result R i ) (R i β) (α, β ); end else done := true; Note: each R i is in BCNF, and decomposition is lossless-join. 8.5
Example of BCNF Decomposition R = (A, B, C ) F = {A B B C} Key = {A} 8.6
Example of BCNF Decomposition class (course_id, title, dept_name, credits, sec_id, semester, year, building, room_number, capacity, time_slot_id) Functional dependencies: course_id title, dept_name, credits building, room_number capacity course_id, sec_id, semester, year building, room_number, time_slot_id A candidate key {course_id, sec_id, semester, year}. 8.7
8.19 Give a lossless-join decomposition into BCNF of schema r(a, B, C,D,E) with respect to the following set F of functional dependencies: A BC CD E B D E A 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 8!
8.29.e Consider the following set F of functional dependencies on the schema r(a, B, C,D,E, F) A BCD BC DE B D D A Give a BCNF decomposition of r. 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 9!
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 This implies that testing for JK L requires a join 8.10
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. (NOTE: each attribute may be in a different candidate key) If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold). Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later). 8.11
3NF Decomposition Algorithm Let F c be a canonical cover for F; i := 0; for each functional dependency α β in F c do if none of the schemas R j, 1 j i contains α β then begin i := i + 1; R i := α β end if none of the schemas R j, 1 j i contains a candidate key for R then begin i := i + 1; R i := any candidate key for R; end /* Optionally, remove redundant relations */ repeat if any schema R j is contained in another schema R k then /* delete R j */ R j = R;; i=i-1; return (R 1, R 2,..., R i ) 8.12
3NF Decomposition: An Example Relation schema: cust_banker_branch = (customer_id, employee_id, branch_name, type ) The functional dependencies for this relation schema are: 1. customer_id, employee_id branch_name, type 2. employee_id branch_name 3. customer_id, branch_name employee_id We first compute a canonical cover branch_name is extraneous in the r.h.s. of the 1 st dependency No other attribute is extraneous, so we get F C = customer_id, employee_id type employee_id branch_name customer_id, branch_name employee_id 8.13
8.20 Give a lossless-join, dependency-preserving decomposition into 3NF of schema r(a, B, C,D,E) with respect to the following set F of functional dependencies: A BC CD E B D E A 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 14!
8.29.d Consider the following set F of functional dependencies on the schema r(a, B, C,D,E, F) A BCD BC DE B D D A Give a 3NF decomposition of r based on the canonical cover 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 15!
Consider the following set F of functional dependencies on the schema r(a, B, C,D,E, F) A B B C A C D E AD E Give a 3NF decomposition of r based on the canonical cover 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 16!
Comparison of BCNF and 3NF It is always possible to decompose a relation into a set of relations that are in 3NF such that: the decomposition is lossless the dependencies are preserved It is always possible to decompose a relation into a set of relations that are in BCNF such that: the decomposition is lossless it may not be possible to preserve dependencies. 8.17
Design Goals Goal for a relational database design is: BCNF. Lossless join. Dependency preservation. If we cannot achieve this, we accept one of Lack of dependency preservation Redundancy due to use of 3NF 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, (and currently not supported by any of the widely used databases!) 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. 8.18
8.11.a In the BCNF decomposition algorithm, suppose you use a functional dependency α β to decompose a relation schema r(α, β, γ) into r1(α, β) and r2(α, γ). What primary and foreignkey constraint do you expect to hold on the decomposed relations? 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 19!
8.11.b In the BCNF decomposition algorithm, suppose you use a functional dependency α β to decompose a relation schema r(α, β, γ) into r1(α, β) and r2(α, γ). Give an example of an inconsistency that can arise due to an erroneous update, if the foreign-key constraint were not enforced on the decomposed relations. 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 20!
8.11.c When a relation is decomposed into 3NF, what primary and foreign key dependencies would you expect will hold on the decomposed schema? 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 21!
8.1 Suppose that we decompose the schema r(a, B, C,D,E) into r1(a, B, C) and r2(a, D, E). Show that this decomposition is a lossless decomposition if the following set F of functional dependencies holds: A BC CD E B D E A 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 22!
8.16 Let a prime attribute be one that appears in at least one candidate key. Let α and β be sets of attributes such that α β, holds, but β α does not hold. Let A be an attribute that is not in α and β, and for which β A holds. We say that A is transitively dependent on α. We can restate our definition of 3NF as follows: A relation schema R is in 3NF with respect to a set F of functional dependencies if there are no nonprime attributes A in R for which A is transitively dependent on a key for R. Show that this new definition is equivalent to textbook definition of 3NF. 16/03/15! Department of Computer Science and Engineering! Indian Institute of Technology Ropar! 23!
Multivalued Dependencies Suppose we record names of children, and phone numbers for instructors: inst_child(id, child_name) inst_phone(id, phone_number) If we were to combine these schemas to get inst_info(id, child_name, phone_number) Example data: (99999, David, 512-555-1234) (99999, David, 512-555-4321) (99999, William, 512-555-1234) (99999, William, 512-555-4321) This relation is in BCNF Why? 8.24
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 ) then < y 1, z 1, w 1 > r and < y 1, z 2, w 2 > r < y 1, z 1, w 2 > r and < y 1, z 2, w 1 > r Note that since the behavior of Z and W are identical it follows that Y Z if Y W 8.25
Example (Cont.) In our example: ID child_name ID phone_number The above formal definition is supposed to formalize the notion that given a particular value of Y (ID) it has associated with it a set of values of Z (child_name) and a set of values of W (phone_number), 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) Z 1 = Z 2 The claim follows. 8.26
MVD (Cont.) Tabular representation of α β 8.27
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 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 β] 8.28
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 relations rʹ that does satisfy the multivalued dependency by adding tuples to r. 8.29
Theory of MVDs From the definition of multivalued dependency, we can derive the following rule: If α β, then α β That is, every functional dependency is also a multivalued dependency The closure D + of D is the set of all functional and multivalued dependencies logically implied by D. We can compute D + from D, using the formal definitions of functional dependencies and multivalued dependencies. We can manage with such reasoning for very simple multivalued dependencies, which seem to be most common in practice For complex dependencies, it is better to reason about sets of dependencies using a system of inference rules (see Appendix C). 8.30
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 8.31
Restriction of Multivalued Dependencies The restriction of D to R i is the set D i consisting of All functional dependencies in D + that include only attributes of R i All multivalued dependencies of the form α (β R i ) where α R i and α β is in D + 8.32
4NF Decomposition Algorithm result: = {R}; done := false; compute D + ; Let D i denote the restriction of D + to R i while (not done) if (there is a schema R i in result that is not in 4NF) then begin let α β be a nontrivial multivalued dependency that holds on R i such that α R i is not in D i, and α β=φ; result := (result - R i ) (R i - β) (α, β); end else done:= true; Note: each R i is in 4NF, and decomposition is lossless-join 8.33
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) R 1 = (A, B) (R 1 is in 4NF) b) R 2 = (A, C, G, H, I) (R 2 is not in 4NF, decompose into R 3 and R 4 ) c) R 3 = (C, G, H) (R 3 is in 4NF) d) R 4 = (A, C, G, I) (R 4 is not in 4NF, decompose into R 5 and R 6 ) A B and B HI è A HI, (MVD transitivity), and and hence A I (MVD restriction to R 4 ) e) R 5 = (A, I) (R 5 is in 4NF) f)r 6 = (A, C, G) (R 6 is in 4NF) 8.34