Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies.

Similar presentations


Presentation on theme: "CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies."— Presentation transcript:

1 CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies (R&G, ch. 19)

2 CMU SCS Faloutsos & PavloSCS 15-415/6152 Functional dependencies motivation: ‘good’ tables takes1 (ssn, c-id, grade, name, address) ‘good’ or ‘bad’?

3 CMU SCS Faloutsos & PavloSCS 15-415/6153 Functional dependencies takes1 (ssn, c-id, grade, name, address)

4 CMU SCS Faloutsos & PavloSCS 15-415/6154 Functional dependencies ‘Bad’ – Q: why?

5 CMU SCS Faloutsos & PavloSCS 15-415/6155 Functional dependencies ‘Bad’ – Q: why? A: Redundancy –space –inconsistencies –insertion/deletion anomalies

6 CMU SCS Faloutsos & PavloCMU SCS 15-415/6156 Pitfalls insertion anomaly: –“jones” registers, but takes no class - no place to store his address!

7 CMU SCS Faloutsos & PavloCMU SCS 15-415/6157 Pitfalls deletion anomaly: –delete the last record of ‘smith’ (we lose his address!)

8 CMU SCS Faloutsos & PavloSCS 15-415/6158 Functional dependencies ‘Bad’ – Q: why? A: Redundancy –space –inconsistencies –insertion/deletion anomalies (later…) Q: What caused the problem?

9 CMU SCS Faloutsos & PavloSCS 15-415/6159 Functional dependencies A: ‘name’ depends on the ‘ssn’ define ‘depends’

10 CMU SCS Faloutsos & PavloSCS 15-415/61510 Overview Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

11 CMU SCS Faloutsos & PavloSCS 15-415/61511 Functional dependencies Definition: ‘a’ functionally determines ‘b’

12 CMU SCS Faloutsos & PavloSCS 15-415/61512 Functional dependencies Informally: ‘if you know ‘a’, there is only one ‘b’ to match’

13 CMU SCS Faloutsos & PavloSCS 15-415/61513 Functional dependencies formally: if two tuples agree on the ‘X’ attribute, the *must* agree on the ‘Y’ attribute, too (eg., if ssn is the same, so should address)

14 CMU SCS Faloutsos & PavloSCS 15-415/61514 Functional dependencies ‘X’, ‘Y’ can be sets of attributes Q: other examples?? (no repeat courses)

15 CMU SCS Faloutsos & PavloSCS 15-415/61515 Functional dependencies ssn -> name, address ssn, c-id -> grade

16 CMU SCS Faloutsos & PavloSCS 15-415/61516 Overview Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

17 CMU SCS Faloutsos & PavloSCS 15-415/61517 Overall goal for both lectures Given tables –STUDENT(ssn, ….) –TAKES( ssn, cid, …) And FD (ssn -> …, cid-> …) WRITE CODE To automatically generate ‘good’ schemas

18 CMU SCS Faloutsos & PavloCMU SCS 15-415/61518 “we want tables where the attributes depend on the primary key, on the whole key, and nothing but the key” Overall goal for both lectures

19 CMU SCS Faloutsos & PavloSCS 15-415/61519 Functional dependencies Closure of a set of FD: all implied FDs - eg.: ssn -> name, address ssn, c-id -> grade imply ssn, c-id -> grade, name, address ssn, c-id -> ssn

20 CMU SCS Faloutsos & PavloSCS 15-415/61520 FDs - Armstrong’s axioms Closure of a set of FD: all implied FDs - eg.: ssn -> name, address ssn, c-id -> grade how to find all the implied ones, systematically?

21 CMU SCS Faloutsos & PavloSCS 15-415/61521 FDs - Armstrong’s axioms “Armstrong’s axioms” guarantee soundness and completeness: Reflexivity: eg., ssn, name -> ssn Augmentation eg., ssn->name then ssn,grade-> name,grade

22 CMU SCS Faloutsos & PavloSCS 15-415/61522 FDs - Armstrong’s axioms Transitivity ssn -> address address -> county-tax-rate THEN: ssn -> county-tax-rate

23 CMU SCS Faloutsos & PavloSCS 15-415/61523 FDs - Armstrong’s axioms Reflexivity: Augmentation: Transitivity: ‘sound’ and ‘complete’

24 CMU SCS Faloutsos & PavloSCS 15-415/61524 FDs - Armstrong’s axioms Additional rules: Union Decomposition Pseudo-transitivity

25 CMU SCS Faloutsos & PavloSCS 15-415/61525 FDs - Armstrong’s axioms Prove ‘Union’ from three axioms:

26 CMU SCS Faloutsos & PavloSCS 15-415/61526 FDs - Armstrong’s axioms Prove ‘Union’ from three axioms:

27 CMU SCS Faloutsos & PavloSCS 15-415/61527 FDs - Armstrong’s axioms Prove Pseudo-transitivity:

28 CMU SCS Faloutsos & PavloSCS 15-415/61528 FDs - Armstrong’s axioms Prove Decomposition

29 CMU SCS Faloutsos & PavloSCS 15-415/61529 Overview Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

30 CMU SCS Faloutsos & PavloSCS 15-415/61530 FDs - Closure F+ Given a set F of FD (on a schema) F+ is the set of all implied FD. Eg., takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address }F}F

31 CMU SCS Faloutsos & PavloSCS 15-415/61531 FDs - Closure F+ ssn, c-id -> grade ssn-> name, address ssn-> ssn ssn, c-id-> address c-id, address-> c-id... F+

32 CMU SCS Faloutsos & PavloSCS 15-415/61532 FDs - Closure A+ Given a set F of FD (on a schema) A+ is the set of all attributes determined by A: takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {ssn}+ =?? }F}F

33 CMU SCS Faloutsos & PavloSCS 15-415/61533 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {ssn}+ ={ssn, }F}F

34 CMU SCS Faloutsos & PavloSCS 15-415/61534 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {ssn}+ ={ssn, name, address } }F}F

35 CMU SCS Faloutsos & PavloSCS 15-415/61535 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {c-id}+ = ?? }F}F

36 CMU SCS Faloutsos & PavloSCS 15-415/61536 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {c-id, ssn}+ = ?? }F}F

37 CMU SCS Faloutsos & PavloSCS 15-415/61537 FDs - Closure A+ if A+ = {all attributes of table} then ‘A’ is a superkey

38 CMU SCS Faloutsos & PavloSCS 15-415/61538 FDs - A+ closure - not in book Diagrams AB->C (1) A->BC (2) B->C (3) A->B (4) C A B Paint ‘A’ ‘red’; For each arrow, paint tip ‘red’, if base is ‘red’

39 CMU SCS Faloutsos & PavloSCS 15-415/61539 FDs - A+ closure - not in book Diagrams AB->C (1) A->BC (2) B->C (3) A->B (4) C A B Paint ‘A’ ‘red’; For each arrow, paint tip ‘red’, if base is ‘red’ Repeat, without fd (2):

40 CMU SCS Faloutsos & PavloSCS 15-415/61540 FDs - ‘canonical cover’ Fc Given a set F of FD (on a schema) Fc is a minimal set of equivalent FD. Eg., takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address ssn,name-> name, address ssn, c-id-> grade, name F

41 CMU SCS Faloutsos & PavloSCS 15-415/61541 FDs - ‘canonical cover’ Fc ssn, c-id -> grade ssn-> name, address ssn,name-> name, address ssn, c-id-> grade, name F Fc takes(ssn, c-id, grade, name, address)

42 CMU SCS Faloutsos & PavloSCS 15-415/61542 FDs - ‘canonical cover’ Fc why do we need it? define it properly compute it efficiently

43 CMU SCS Faloutsos & PavloSCS 15-415/61543 FDs - ‘canonical cover’ Fc why do we need it? –easier to compute candidate keys define it properly compute it efficiently

44 CMU SCS Faloutsos & PavloSCS 15-415/61544 FDs - ‘canonical cover’ Fc define it properly - three properties –1) the RHS of every FD is a single attribute –2) the closure of Fc is identical to the closure of F (ie., Fc and F are equivalent) –3) Fc is minimal (ie., if we eliminate any attribute from the LHS or RHS of a FD, property #2 is violated

45 CMU SCS Faloutsos & PavloSCS 15-415/61545 #3: we need to eliminate ‘extraneous’ attributes. An attribute is ‘extraneous if – the closure is the same, before and after its elimination –or if F-before implies F-after and vice-versa FDs - ‘canonical cover’ Fc

46 CMU SCS Faloutsos & PavloSCS 15-415/61546 FDs - ‘canonical cover’ Fc ssn, c-id -> grade ssn-> name, address ssn,name-> name, address ssn, c-id-> grade, name F

47 CMU SCS Faloutsos & PavloSCS 15-415/61547 FDs - ‘canonical cover’ Fc Algorithm: examine each FD; drop extraneous LHS or RHS attributes; or redundant FDs make sure that FDs have a single attribute in their RHS repeat until no change

48 CMU SCS Faloutsos & PavloSCS 15-415/61548 FDs - ‘canonical cover’ Fc Trace algo for AB->C (1) A->BC (2) B->C (3) A->B (4)

49 CMU SCS Faloutsos & PavloSCS 15-415/61549 FDs - ‘canonical cover’ Fc Trace algo for AB->C (1) A->BC (2) B->C (3) A->B (4) split (2): AB->C (1) A->B (2’) A->C (2’’) B->C (3) A->B (4)

50 CMU SCS Faloutsos & PavloSCS 15-415/61550 FDs - ‘canonical cover’ Fc AB->C (1) A->B (2’) A->C (2’’) B->C (3) A->B (4) AB->C (1) A->C (2’’) B->C (3) A->B (4)

51 CMU SCS Faloutsos & PavloSCS 15-415/61551 FDs - ‘canonical cover’ Fc AB->C (1) A->C (2’’) B->C (3) A->B (4) (2’’): redundant (implied by (4), (3) and transitivity AB->C (1) B->C (3) A->B (4)

52 CMU SCS Faloutsos & PavloSCS 15-415/61552 FDs - ‘canonical cover’ Fc B->C (1’) B->C (3) A->B (4) AB->C (1) B->C (3) A->B (4) in (1), ‘A’ is extraneous: (1),(3),(4) imply (1’),(3),(4), and vice versa

53 CMU SCS Faloutsos & PavloSCS 15-415/61553 FDs - ‘canonical cover’ Fc B->C (3) A->B (4) B->C (1’) B->C (3) A->B (4) nothing is extraneous all RHS are single attributes final and original set of FDs are equivalent (same closure)

54 CMU SCS Faloutsos & PavloSCS 15-415/61554 FDs - ‘canonical cover’ Fc AFTER B->C (3) A->B (4) BEFORE AB->C (1) A->BC (2) B->C (3) A->B (4) R(A,B,C)

55 CMU SCS Faloutsos & PavloSCS 15-415/61555 Overview - conclusions Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover


Download ppt "CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications Lecture #16: Schema Refinement & Normalization - Functional Dependencies."

Similar presentations


Ads by Google