Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-usewww.db-book.com Chapter 7: Relational.

Similar presentations


Presentation on theme: "Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-usewww.db-book.com Chapter 7: Relational."— Presentation transcript:

1 Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-usewww.db-book.com Chapter 7: Relational Database Design

2 ©Silberschatz, Korth and Sudarshan7.2Database System Concepts - 5 th Edition, Oct 5, 2006 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 ©Silberschatz, Korth and Sudarshan7.3Database System Concepts - 5 th Edition, Oct 5, 2006 The Banking Schema branch = (branch_name, branch_city, assets) customer = (customer_id, customer_name, customer_street, customer_city) loan = (loan_number, amount) account = (account_number, balance) employee = (employee_id. employee_name, telephone_number, start_date) dependent_name = (employee_id, dname) account_branch = (account_number, branch_name) loan_branch = (loan_number, branch_name) borrower = (customer_id, loan_number) depositor = (customer_id, account_number) cust_banker = (customer_id, employee_id, type) works_for = (worker_employee_id, manager_employee_id) payment = (loan_number, payment_number, payment_date, payment_amount) savings_account = (account_number, interest_rate) checking_account = (account_number, overdraft_amount)

4 ©Silberschatz, Korth and Sudarshan7.4Database System Concepts - 5 th Edition, Oct 5, 2006 Combine Schemas? n Suppose we combine borrower and loan to get bor_loan = (customer_id, loan_number, amount ) n Result is possible repetition of information (L-100 in example below)

5 ©Silberschatz, Korth and Sudarshan7.5Database System Concepts - 5 th Edition, Oct 5, 2006 A Combined Schema Without Repetition n Consider combining loan_branch and loan loan_amt_br = (loan_number, amount, branch_name) n No repetition (as suggested by example below) n Problem arises specifically the repetition of information when the join attribute was not the primary key for both schemas being combined.

6 ©Silberschatz, Korth and Sudarshan7.6Database System Concepts - 5 th Edition, Oct 5, 2006 What About Smaller Schemas? n Suppose we had started with bor_loan. How would we know to split up (decompose) it into borrower and loan? n Write a rule “if there were a schema (loan_number, amount), then loan_number would be a candidate key” n Denote as a functional dependency: loan_number  amount n In bor_loan, because loan_number is not a candidate key, the amount of a loan may have to be repeated. This indicates the need to decompose bor_loan. n Not all decompositions are good. Suppose we decompose employee into employee1 = (employee_id, employee_name) employee2 = (employee_name, telephone_number, start_date) n The next slide shows how we lose information -- we cannot reconstruct the original employee relation -- and so, this is a lossy decomposition

7 ©Silberschatz, Korth and Sudarshan7.7Database System Concepts - 5 th Edition, Oct 5, 2006 A Lossy Decomposition

8 ©Silberschatz, Korth and Sudarshan7.8Database System Concepts - 5 th Edition, Oct 5, 2006 First Normal Form n Domain is atomic if its elements are considered to be indivisible units l Examples of non-atomic domains:  Set of names, composite attributes  Identification numbers like CS101 that can be broken up into parts n A relational schema R is in first normal form if the domains of all attributes of R are atomic n Non-atomic values complicate storage and encourage redundant (repeated) storage of data l Example: Set of accounts stored with each customer, and set of owners stored with each account n We assume all relations are in first normal form (and revisit this in Chapter 9)

9 ©Silberschatz, Korth and Sudarshan7.9Database System Concepts - 5 th Edition, Oct 5, 2006 First Normal Form (Cont’d) n Atomicity is actually a property of how the elements of the domain are used. l Example: Strings would normally be considered indivisible l Suppose that students are given roll numbers which are strings of the form CS0012 or EE1127 l If the first two characters are extracted to find the department, the domain of roll numbers is not atomic. l Doing so is a bad idea: leads to encoding of information in application program rather than in the database.

10 ©Silberschatz, Korth and Sudarshan7.10Database System Concepts - 5 th Edition, Oct 5, 2006 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,..., R n } such that l each relation is in good form l the decomposition is a lossless-join decomposition n Our theory is based on: l functional dependencies l multivalued dependencies

11 ©Silberschatz, Korth and Sudarshan7.11Database System Concepts - 5 th Edition, Oct 5, 2006 Functional Dependencies n Constraints on the set of legal relations. n Require that the value for a certain set of attributes determines uniquely the value for another set of attributes. n A functional dependency is a generalization of the notion of a key.

12 ©Silberschatz, Korth and Sudarshan7.12Database System Concepts - 5 th Edition, Oct 5, 2006 Functional Dependencies (Cont.) n Let R be a relation schema   R and   R n 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 [  ] n Example: Consider r(A,B ) with the following instance of r. n On this instance, l Is A  B?  NOT l Is B  A ?  Holds A B 1 4 1 5 37

13 ©Silberschatz, Korth and Sudarshan7.13Database System Concepts - 5 th Edition, Oct 5, 2006 Functional Dependencies (Cont.) ABCD A1B1C1D1 A1B2C1D2 A2B2C2D2 A2B3C2D3 A3B3C2D4 On this instance, C  A does NOT hold A  C does hold.

14 ©Silberschatz, Korth and Sudarshan7.14Database System Concepts - 5 th Edition, Oct 5, 2006 Functional Dependencies (Cont.) n K is a superkey for relation schema R if and only if K  R n K is a candidate key for R if and only if l K  R, and l for no   K,   R n 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

15 ©Silberschatz, Korth and Sudarshan7.15Database System Concepts - 5 th Edition, Oct 5, 2006 Use of Functional Dependencies n We use functional dependencies to: l test relations to see if they are legal under a given set of functional dependencies.  If a relation r is legal under a set F of functional dependencies, we say that r satisfies F. l specify constraints on the set of legal relations  We say that F holds on R if all legal relations on R satisfy the set of functional dependencies F. n Note: A specific instance of a relation schema may satisfy a functional dependency even if the functional dependency does not hold on all legal instances. l For example, a specific instance of loan may, by chance, satisfy amount  customer_name.

16 ©Silberschatz, Korth and Sudarshan7.16Database System Concepts - 5 th Edition, Oct 5, 2006 Functional Dependencies (Cont.) n A functional dependency is trivial if it is satisfied by all instances of a relation l Example:  customer_name, loan_number  customer_name  customer_name  customer_name l In general,    is trivial if   

17 ©Silberschatz, Korth and Sudarshan7.17Database System Concepts - 5 th Edition, Oct 5, 2006 Closure of a Set of Functional Dependencies n Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F. l For example: If A  B and B  C, then we can infer that A  C n The set of all functional dependencies logically implied by F is the closure of F. n We denote the closure of F by F +. n F + is a superset of F.

18 ©Silberschatz, Korth and Sudarshan7.18Database System Concepts - 5 th Edition, Oct 5, 2006 Example ABC a1b1C1 a1b1C2 a2b1C1 a2b1C3 List all non trivial functional dependencies A  B C  B AC  B

19 ©Silberschatz, Korth and Sudarshan7.19Database System Concepts - 5 th Edition, Oct 5, 2006 Boyce-Codd Normal Form n     is trivial (i.e.,    ) n  is a superkey for R 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: Example schema not in BCNF: bor_loan = ( customer_id, loan_number, amount ) because loan_number  amount holds on bor_loan but loan_number is not a superkey

20 ©Silberschatz, Korth and Sudarshan7.20Database System Concepts - 5 th Edition, Oct 5, 2006 Decomposing a Schema into BCNF n Suppose we have a schema R and a non-trivial dependency     causes a violation of BCNF. We decompose R into: (   U  ) ( R - (  -  ) ) n In our example, l  = loan_number l  = amount and bor_loan is replaced by l (   U  ) = ( loan_number, amount ) l ( R - (  -  ) ) = ( customer_id, loan_number )

21 ©Silberschatz, Korth and Sudarshan7.21Database System Concepts - 5 th Edition, Oct 5, 2006 Example Example schema not in BCNF: instr_dept (ID, name, salary, dept_name, building ) because dept_name  building holds on instr_dept, but dept_name is not a superkey n In our example, l  = dept_name l  = building and inst_dept is replaced by l (   U  ) = ( dept_name, building ) l ( R - (  -  ) ) = ( ID, name, salary, dept_name )

22 ©Silberschatz, Korth and Sudarshan7.22Database System Concepts - 5 th Edition, Oct 5, 2006 Closure of a Set of Functional Dependencies n Given a set F set of functional dependencies, there are certain other functional dependencies that are logically implied by F. l For example: If A  B and B  C, then we can infer that A  C n The set of all functional dependencies logically implied by F is the closure of F. n We denote the closure of F by F +. n We can find all of F + by applying Armstrong’s Axioms: l if   , then    (reflexivity) l if   , then      (augmentation) l if   , and   , then    (transitivity) n These rules are l sound (do not generate any incorrect functional dependencies) and l complete (generate all functional dependencies that hold).

23 ©Silberschatz, Korth and Sudarshan7.23Database System Concepts - 5 th Edition, Oct 5, 2006 Example n R = (A, B, C, G, H, I) F = { A  B A  C CG  H CG  I B  H} n some members of F + l A  H  by transitivity from A  B and B  H l AG  I  by augmenting A  C with G, to get AG  CG and then transitivity with CG  I l CG  HI  by augmenting CG  I to infer CG  CGI, and augmenting of CG  H to infer CGI  HI, and then transitivity

24 ©Silberschatz, Korth and Sudarshan7.24Database System Concepts - 5 th Edition, Oct 5, 2006 Procedure for Computing F + n 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 shall see an alternative procedure for this task later

25 ©Silberschatz, Korth and Sudarshan7.25Database System Concepts - 5 th Edition, Oct 5, 2006 Closure of Functional Dependencies (Cont.) n We can further simplify manual computation of F + by using the following additional rules. l If    holds and    holds, then     holds (union) l If     holds, then    holds and    holds (decomposition) l If    holds and     holds, then     holds (pseudotransitivity) The above rules can be inferred from Armstrong’s axioms.

26 ©Silberschatz, Korth and Sudarshan7.26Database System Concepts - 5 th Edition, Oct 5, 2006 Closure of Attribute Sets n Given a set of attributes a, define the closure of a under F (denoted by a + ) as the set of attributes that are functionally determined by a under F n Algorithm to compute a +, the closure of a under F result := a; while (changes to result) do for each    in F do begin if   result then result := result   end

27 ©Silberschatz, Korth and Sudarshan7.27Database System Concepts - 5 th Edition, Oct 5, 2006 Example of Attribute Set Closure n R = (A, B, C, G, H, I) n F = {A  B A  C CG  H CG  I B  H} n Compute (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) n Is AG a candidate key? 1. Is AG a super key? 1. Does AG  R? == Is (AG) +  R 2. Is any subset of AG a superkey? 1. Does A  R? == Is (A) +  R 2. Does G  R? == Is (G) +  R

28 ©Silberschatz, Korth and Sudarshan7.28Database System Concepts - 5 th Edition, Oct 5, 2006 Uses of Attribute Closure There are several uses of the attribute closure algorithm: n Testing for superkey: l To test if  is a superkey, we compute  +, and check if  + contains all attributes of R. n Testing functional dependencies l To check if a functional dependency    holds (or, in other words, is in F + ), just check if    +. l That is, we compute  + by using attribute closure, and then check if it contains . l Is a simple and cheap test, and very useful n Computing closure of F l For each   R, we find the closure  +, and for each S   +, we output a functional dependency   S.

29 ©Silberschatz, Korth and Sudarshan7.29Database System Concepts - 5 th Edition, Oct 5, 2006 Example n R = (A, B, C, D, E) n F = {A  BC CD  E B  D E  A} Compute (A) + n Starting with A → BC, we can conclude: A → B and A → C. n Since A → B and B → D, A → D (decomposition, transitive) n Since A → CD and CD → E, A → E (union, decomposition, transitive) n Since A → A, we have (reflexive) n A → ABCDE from the above steps (union) n List all the candidate keys n A, E, BC, CD

30 ©Silberschatz, Korth and Sudarshan7.30Database System Concepts - 5 th Edition, Oct 5, 2006 Canonical Cover n Sets of functional dependencies may have redundant dependencies that can be inferred from the others l For example: A  C is redundant in: {A  B, B  C} l 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} n Intuitively, a canonical cover of F is a “minimal” set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies

31 ©Silberschatz, Korth and Sudarshan7.31Database System Concepts - 5 th Edition, Oct 5, 2006 Extraneous Attributes n Consider a set F of functional dependencies and the functional dependency    in F. l Attribute A is extraneous in  if A   and F logically implies (F – {    })  {(  – A)   }. l Attribute A is extraneous in  if A   and the set of functional dependencies (F – {    })  {   (  – A)} logically implies F. n Example: Given F = {A  C, AB  C } l B is extraneous in AB  C because {A  C, AB  C} logically implies A  C (I.e. the result of dropping B from AB  C). n Example: Given F = {A  C, AB  CD} l C is extraneous in AB  CD

32 ©Silberschatz, Korth and Sudarshan7.32Database System Concepts - 5 th Edition, Oct 5, 2006 Testing if an Attribute is Extraneous n Consider a set F of functional dependencies and the functional dependency    in F. n To test if attribute A   is extraneous in  1. compute ({  } – A) + using the dependencies in F 2. check that ({  } – A) + contains  ; if it does, A is extraneous in  n To test if attribute A   is extraneous in  1. compute  + using only the dependencies in F’ = (F – {    })  {   (  – A)}, 2. check that  + contains A; if it does, A is extraneous in 

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

34 ©Silberschatz, Korth and Sudarshan7.34Database System Concepts - 5 th Edition, Oct 5, 2006 Computing a Canonical Cover n R = (A, B, C) F = {A  BC B  C A  B AB  C} n Combine A  BC and A  B into A  BC l Set is now {A  BC, B  C, AB  C} n A is extraneous in AB  C l Check if the result of deleting A from AB  C is implied by the other dependencies  Yes: in fact, B  C is already present! l Set is now {A  BC, B  C} n C is extraneous in A  BC l Check if A  C is logically implied by A  B and the other dependencies  Yes: using transitivity on A  B and B  C. –Can use attribute closure of A in more complex cases n The canonical cover is: A  B B  C

35 ©Silberschatz, Korth and Sudarshan7.35Database System Concepts - 5 th Edition, Oct 5, 2006 Canonical Cover Algorithm Example: determine the canonical cover of F = {A  BC, B  CE, A  E} Iteration 1: a. F = { A  BCE, B  CE} b. Must check for upto 5 extraneous attributes - B extraneous in A  BCE? - C extraneous in A  BCE? (A  C) in { A  BE, B  CE} 1. A  BE -> 2. A  B -> 3. A  CE -> 4. A  C -E extraneous in A  BE? 1. A  B -> 2. A  CE -> A  E - E extraneous in B  CE - C extraneous in B  CE F c = { A  B, B  CE} No Yes No

36 ©Silberschatz, Korth and Sudarshan7.36Database System Concepts - 5 th Edition, Oct 5, 2006 Practice Given: F = { A  BC, B  CE, A  E, AC  H, D  B} Determines canonical cover of F: F c = { A  BH, B  CE, D  B} Another example: F = { A  BC, B  C, A  B, AB  C, AC  D} F c = { A  BD, B  C}

37 Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-usewww.db-book.com End of Chapter


Download ppt "Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-usewww.db-book.com Chapter 7: Relational."

Similar presentations


Ads by Google