Presentation is loading. Please wait.

Presentation is loading. Please wait.

7.1 Chapter 7: Relational Database Design. 7.2 Chapter 7: Relational Database Design Features of Good Relational Design Atomic Domains and First Normal.

Similar presentations


Presentation on theme: "7.1 Chapter 7: Relational Database Design. 7.2 Chapter 7: Relational Database Design Features of Good Relational Design Atomic Domains and First Normal."— Presentation transcript:

1 7.1 Chapter 7: Relational Database Design

2 7.2 Chapter 7: Relational Database Design Features of Good Relational Design Atomic Domains and First Normal Form Decomposition Using Functional Dependencies Functional Dependency Theory Algorithms for Functional Dependencies Decomposition Using Multivalued Dependencies More Normal Form Database-Design Process Modeling Temporal Data

3 7.3 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.

4 7.4 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 (functional dependency: branch-name  branch-city assets) Wastes space Complicates updating, introducing possibility of inconsistency of assets value Null values Cannot store information about a branch if no loans exist Can use null values, but they are difficult to handle.

5 7.5 Decomposition Decompose the relation schema Lending-schema into: Branch-schema = (branch-name, branch-city,assets) Loan-info-schema = (customer-name, loan-number, branch-name, amount) All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): R = R 1  R 2 Lossless-join decomposition. For all possible relations r on schema R r =  R1 (r)  R2 (r)

6 7.6 A Lossy-join Decomposition

7 7.7 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 {R 1, R 2,..., R n } such that each relation is in good form the decomposition is a lossless-join decomposition Our theory is based on: functional dependencies multivalued dependencies

8 7.8 Functional Dependencies Constraints on the set of legal relations. Require that the value for a certain set of attributes determines uniquely the value for another set of attributes. A functional dependency is a generalization of the notion of a key.

9 7.9 Functional Dependencies (Cont.) Let R be a relation schema   R and   R The functional dependency    holds on R if and only if for any legal relations r(R), whenever any two tuples t 1 and t 2 of r agree on the attributes , they also agree on the attributes . That is, t 1 [  ] = t 2 [  ]  t 1 [  ] = t 2 [  ] Example: Consider r(A,B ) with the following instance of r. On this instance, A  B does NOT hold, but B  A does hold. 14 1 5 37

10 7.10 Functional Dependencies (Cont.) K is a superkey for relation schema R if and only if K  R K is a candidate key for R if and only if K  R, and for no   K,   R Functional dependencies allow us to express constraints that cannot be expressed using superkeys. Consider the schema: bor_loan = (customer_id, loan_number, amount ). We expect this functional dependency to hold: loan_number  amount but would not expect the following to hold: amount  customer_name

11 7.11 Functional Dependencies (Cont.) A functional dependency is trivial if it is satisfied by all instances of a relation Example:  customer_name, loan_number  customer_name  customer_name  customer_name In general,    is trivial if    When we design a relational database, we first list those functional dependencies that must always hold The list of functional dependencies in banking database (next page)

12 7.12 Functional dependencies in banking database On Branch-schema branch-name  branch-city branch-name  assets On Customer-schema customer-name  branch-city customer-name  customer-street On Loan-schema loan-number  amount loan-number  branch-name On Account-schema account-number  branch-name account-number  balance

13 7.13 Closure of a Set of Functional Dependencies Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F. E.g. If A  B and B  C, then we can infer that A  C The set of all functional dependencies logically implied by F is the closure of F. We denote the closure of F by F +. We can find all of F + by applying Armstrong’s Axioms: if   , then    (reflexivity) if   , then      (augmentation) if   , and   , then    (transitivity) These rules are sound (generate only functional dependencies that actually hold) and complete (generate all functional dependencies that hold).

14 7.14 Example R = (A, B, C, G, H, I) F = { A  B A  C CG  H CG  I B  H} some members of F + A  H  by transitivity from A  B and B  H AG  I  by augmenting A  C with G, to get AG  CG and then transitivity with CG  I CG  HI  from CG  H and CG  I : “union rule” can be inferred from –Augmentation of CG  I to infer CG  CGI, augmentation of CG  H to infer CGI  HI, and then transitivity

15 7.15 Procedure for Computing F + To compute the closure of a set of functional dependencies F: F + = F repeat for each functional dependency f in F + apply reflexivity and augmentation rules on f add the resulting functional dependencies to F + for each pair of functional dependencies f 1 and f 2 in F + if f 1 and f 2 can be combined using transitivity then add the resulting functional dependency to F + until F + does not change any further NOTE: We will see an alternative procedure for this task later

16 7.16 Closure of Functional Dependencies We can further simplify manual computation of F + by using the following additional rules. If    holds and    holds, then     holds (union) If     holds, then    holds and    holds (decomposition) If    holds and     holds, then     holds (pseudotransitivity) The above rules can be inferred from Armstrong’s axioms.

17 7.17 Closure of Attribute Sets Given a set of attributes  define the closure of  under F (denoted by  + ) as the set of attributes that are functionally determined by  under F:    is in F +     + Algorithm to compute  +, the closure of  under F result :=  ; while (changes to result) do for each    in F do begin if   result then result := result   end

18 7.18 Example of Attribute Set Closure R = (A, B, C, G, H, I) F = {A  B A  C CG  H CG  I B  H} (AG) + 1.result = AG 2.result = ABCG(A  C and A  B) 3.result = ABCGH(CG  H and CG  AGBC) 4.result = ABCGHI(CG  I and CG  AGBCH) Is AG a candidate key? 1. Is AG a super key? 1. Does AG  R? 2. Is any subset of AG a superkey? 1. Does A +  R? 2. Does G +  R?

19 7.19 Uses of Attribute Closure There are several uses of the attribute closure algorithm: Testing for superkey: To test if  is a superkey, we compute  +, and check if  + contains all attributes of R. Testing functional dependencies To check if a functional dependency    holds (or, in other words, is in F + ), just check if    +. That is, we compute  + by using attribute closure, and then check if it contains . Is a simple and cheap test, and very useful Computing closure of F For each   R, we find the closure  +, and for each S   +, we output a functional dependency   S.

20 7.20 Decomposition Figure 7.1: (a bad database design) Lending -schema = (branch-name, branch-city, assets, customer-name, loan-number, amount) Repetition of information (add a new loan) Inability to represent certain information (a branch no loan) Observation: branch-name  branch-city branch-name  assets but not branch-name  loan-number From above example, we should decompose a relation schema into several schema with fewer attributes

21 7.21 Decomposition Lending-schema is decomposed into two schemas: Branch-customer-schema = (branch-name, branch-city, assets, customer-name) Customer-loan-schema = (customer-name, loan-number, amount) branch-customer =  branch-name, branch-city, assets, customer-name (Lending) customer-loan =  customer-name, loan-number, amount  (Lending) Figure7.9 and Figure 7.10 We wish to find all branches that have loans with amount less than $1000: Branch-customer  Customer-loan (Figure 7.11) We are no longer able to represent in the database information about which customers are borrowers from which branch

22 7.22 database operating systems Avi Hank Sudarshan Avi Pete DB Concepts Ullman DB Concepts Ullman DB Concepts Ullman OS Concepts Stallings OS Concepts Stallings classes Lending

23 7.23 Branch-customer Customer-loan

24 7.24 Branch-customer Customer-loan

25 7.25 Because of this loss of information, we call the decomposition of Lending-schema into Branch-customer- schema and Customer-loan-schema a lossy decomposition, or a lossy-join decomposition A decomposition that is not a lossy-join decomposition is a lossless-join decomposition Consider another alternative design, in which Lending- schema is decomposed into two schemas: Branch-schema = (branch-name, branch-city, assets) Loan-info-schema = (branch-name, customer-name, loan-number, amount) This decomposition is a lossless-join decomposition, because branch-name  branch-city assets Decomposition

26 7.26 Decomposition All attributes of an original schema (R) must appear in the decomposition (R 1, R 2 ): R = R 1  R 2 Lossless-join decomposition. For all possible relations r on schema R r =  R1 (r)  R2 (r) A decomposition of R into R 1 and R 2 is lossless join if and only if at least one of the following dependencies is in F + : R 1  R 2  R 1 R 1  R 2  R 2

27 7.27 Decomposition Example: Lending-schema = (branch-name, branch-city, assets, customer-name, loan-number, amount). The set F of functional dependencies that we require to hold on Lending-schema are branch-name  branch-city assets loan-number  amount branch-name We decompose Lending-schema into two schemas Branch-schema = (branch-name, branch-city, assets) Loan-info-schema = (branch-name, customer-name, loan-number, amount) Since branch-name  branch-name branch-city assets This decomposition is a lossless-join decomposition Next, we decompose Loan-info-schema into Loan-schema = (branch-name, loan-number, amount) borrower-schema = (customer-name, loan-number) Since loan-number  amount branch-name This decomposition is a lossless-join decomposition

28 7.28 Dependency Preservation When an update is made to the database, the system should be able to check that the update will satisfy all the given functional dependencies To check updates efficiently, we should design relational database schemas that allow update validation without the computation of joins F : a set of functional dependencies on a schema R R 1, R 2, …, R n : a decomposition of R F i : a subset of all functional dependencies in F + that include only attributes of R i The set of restrictions F 1, F 2, …, F n is the set of dependencies that can be checked efficiently Let F’ = F 1  F 2  …  F n Dependency preserving decomposition: A decomposition has the property F’ + = F +

29 7.29 Normalization Using Functional Dependencies When we decompose a relation schema R with a set of functional dependencies F into R 1, R 2,.., R n we want Lossless-join decomposition: Otherwise decomposition would result in information loss. No redundancy: The relations R i preferably should be in either Boyce-Codd Normal Form or Third Normal Form. Dependency preservation: Let F i be the set of dependencies F + that include only attributes in R i.  Preferably the decomposition should be dependency preserving, that is, (F 1  F 2  …  F n ) + = F +  Otherwise, checking updates for violation of functional dependencies may require computing joins, which is expensive.

30 7.30 Example R = (A, B, C) F = {A  B, B  C) R 1 = (A, B), R 2 = (B, C) Lossless-join decomposition: R 1  R 2 = {B} and B  BC Dependency preserving R 1 = (A, B), R 2 = (A, C) Lossless-join decomposition: R 1  R 2 = {A} and A  AB Not dependency preserving (cannot check B  C without computing R 1 R 2 )

31 7.31 Testing for Dependency Preservation Algorithm for testing if a decomposition D is dependency preservation: compute F + for each schema R i in D do begin Fi := the restriction of F + to R i ; end F’ :=  for each restriction F i do begin F’ = F’  Fi end compute F’ + ; if (F’ + = F+) then return (true) else return (false);

32 7.32 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 A database design is in BCNF if each relation schema in the database is in BCNF

33 7.33 Example Customer-schema = (customer-name, customer-street, customer-city) customer-name  customer-street customer-city Customer-schema is in BCNF Branch-schema = (branch-name, assets, branch-city) branch-name  assets branch-city Branch-schema is in BCNF Loan -info-schema = (branch-name, customer-name, loan- number, amount) loan-number  branch-name amount Loan-info-schema is not in BCNF Loan-info-schema suffers from the problem of information repetition: (Downtown, Mr. Bell, L-44, 1000) (Downtown, Ms. Bell, L-44, 1000) Decompose Loan-info-schema into two schemas: Loan-schema = (branch-name, loan-number, amount) Borrower-schema = (customer-name, loan-number) This decomposition is a lossless-join decomposition

34 7.34 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 with a given set of functional dependencies F 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 +. We can show that 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, using only F is incorrect when testing a relation in a decomposition of R E.g. Consider R (A, B, C, D), with F = { A  B, B  C}  Decompose R into R 1 (A,B) and R 2 (A,C,D)  Neither of the dependencies in F contain only attributes from (A,C,D) so we might be mislead into thinking R 2 satisfies BCNF.  In fact, dependency A  C in F + shows R 2 is not in BCNF.

35 7.35 Testing for BCNF An alternative BCNF test is sometimes easier than computing every dependency in F + To check if a relation R i in a decomposition of R is in BCNF, we apply this test For every subset  of attributes in R i, check that  + (the attribute closure of  under F) either includes no attribute of R i - , or includes all attributes of R i If the condition is violated by some set of attributes  in R i, the following functional dependency must be in F +    (  + -  )  R i

36 7.36 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. Since the dependency   holds on R i, schema R i is decomposed into (R i -  ) and ( ,  ), and (R i -  )  ( ,  ) = 

37 7.37 Example of BCNF Decomposition Lending-schema = (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} For the dependency branch-name  assets branch-city, Lending- schema is not in BCNF: Branch = (branch-name, branch-city, assets) Loan-info = (branch-name, customer-name, loan-number, amount) For the dependency loan-number  branch-name amount, Loan- info is not in BCNF: Loan = (branch-name, loan-number, amount) is in BCNF Borrower = (customer-name, loan-number) is in BCNF Not every BCNF decomposition is dependency preserving

38 7.38 Example of BCNF Decomposition Banker-schema = (branch-name, customer-name, banker-name) banker-name  branch-name branch-name customer-name  banker-name Since banker-name is not a superkey, Banker-schema is not in BCNF The BCNF decomposition: Banker-branch = (banker-name, branch-name) Customer-banker = (customer-name, banker-name) This decomposition is not dependency preserving: F 1 = {banker-name  branch-name} F 2 =  F’ = {banker-name  branch-name} F’ +  F + Hence, this decomposition is not dependency preserving

39 7.39 Example of BCNF Decomposition R = (A, B, C ) F = {A  B B  C} Key = {A} R is not in BCNF (B  C but B is not superkey) Decomposition R 1 = (B, C) R 2 = (A,B)

40 7.40 Example of BCNF Decomposition Original relation R and functional dependency F 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 R 1 = (branch_name, branch_city, assets ) R 2 = (branch_name, customer_name, loan_number, amount ) R 3 = (branch_name, loan_number, amount ) R 4 = (customer_name, loan_number ) Final decomposition R 1, R 3, R 4

41 7.41 BCNF and Dependency Preservation 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 It is not always possible to get a BCNF decomposition that is dependency preserving

42 7.42 Third Normal Form: Motivation There are some situations where BCNF is not dependency preserving, and efficient checking for FD violation on updates is important Solution: define a weaker normal form, called Third Normal Form (3NF) Allows some redundancy (with resultant problems; we will see examples later) But functional dependencies can be checked on individual relations without computing a join. There is always a lossless-join, dependency-preserving decomposition into 3NF.

43 7.43 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.

44 7.44 3NF Example Relation R: R = (J, K, L ) F = {JK  L, L  K } Two candidate keys: JK and JL R is in 3NF JK  LJK is a superkey L  KK is contained in a candidate key

45 7.45 Redundancy in 3NF J j 1 j 2 j 3 null L l1l1l1l2l1l1l1l2 K k1k1k1k2k1k1k1k2 A schema that is in 3NF 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). There is some redundancy in this schema Example of problems due to redundancy in 3NF R = (J, K, L) F = {JK  L, L  K }

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

47 7.47 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 return (R 1, R 2,..., R i )

48 7.48 3NF Decomposition Algorithm (Cont.) Above algorithm ensures: each relation schema R i is in 3NF decomposition is dependency preserving and lossless-join

49 7.49 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}

50 7.50 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.

51 7.51 Example Relation schema: cust_banker_branch = (customer_id, employee_id, branch_name, type ) The functional dependencies for this relation schema are: customer_id, employee_id  branch_name, type employee_id  branch_name The for loop generates: (customer_id, employee_id, branch_name, type ) It then generates (employee_id, branch_name) but does not include it in the decomposition because it is a subset of the first schema.

52 7.52 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.

53 7.53 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

54 7.54 Canonical Cover Sets of functional dependencies may have redundant dependencies that can be inferred from the others Eg: A  C is redundant in: {A  B, B  C, A  C} Parts of a functional dependency may be redundant  E.g. on RHS: {A  B, B  C, A  CD} can be simplified to {A  B, B  C, A  D}  E.g. on LHS: {A  B, B  C, AC  D} can be simplified to {A  B, B  C, A  D} Intuitively, a canonical cover of F is a “minimal” set of functional dependencies equivalent to F, with no redundant dependencies or having redundant parts of dependencies

55 7.55 Extraneous Attributes Consider a set F of functional dependencies and the functional dependency    in F. Attribute A is extraneous in  if A   and F logically implies (F – {    })  {(  – A)   }. Attribute A is extraneous in  if A   and the set of functional dependencies (F – {    })  {   (  – A)} logically implies F. Example: Given F = {A  C, AB  C } B is extraneous in AB  C because A  C logically implies AB  C. Example: Given F = {A  C, AB  CD} C is extraneous in AB  CD since A  C can be inferred even after deleting C

56 7.56 Testing if an Attribute is Extraneous Consider a set F of functional dependencies and the functional dependency    in F. To test if attribute A   is extraneous in  (check if (  – {A})   ) 1. compute (  – {A}) + using the dependencies in F 2. check that (  – {A}) + contains  ; if it does, A is extraneous To test if attribute A   is extraneous in  1. compute  + using only the dependencies in F’ = (F – {    })  {   (  – A)}, (check if   A can be inferred from F’) 2. check that  + contains A; if it does, A is extraneous Example: R = (A, B, C, D, E) F = { AB  CD A  E E  C } To check if C is extraneous in AB  CD

57 7.57 Canonical Cover A canonical cover for F is a set of dependencies F c such that F logically implies all dependencies in F c, and F c logically implies all dependencies in F, and No functional dependency in F c contains an extraneous attribute, and Each left side of functional dependency in F c is unique. To compute a canonical cover for F: F c = F repeat Use the union rule to replace any dependencies in F c of the form  1   1 and  1   2 with  1   1  2 Find a functional dependency    in F c with an extraneous attribute either in  or in  If an extraneous attribute is found, delete it from    until F c does not change Note: Union rule may become applicable after some extraneous attributes have been deleted, so it has to be re-applied

58 7.58 Example of Computing a Canonical Cover R = (A, B, C) F = {A  BC B  C A  B AB  C} Combine A  BC and A  B into A  BC Set is now {A  BC, B  C, AB  C} A is extraneous in AB  C because B  C logically implies AB  C. Set is now {A  BC, B  C} C is extraneous in A  BC since A  BC is logically implied by A  B and B  C. The canonical cover is: A  B B  C

59 Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan End of Chapter


Download ppt "7.1 Chapter 7: Relational Database Design. 7.2 Chapter 7: Relational Database Design Features of Good Relational Design Atomic Domains and First Normal."

Similar presentations


Ads by Google