Presentation is loading. Please wait.

Presentation is loading. Please wait.

Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Integrity Constraints.

Similar presentations


Presentation on theme: "Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Integrity Constraints."— Presentation transcript:

1 Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Integrity Constraints

2 Carnegie Mellon 15-415 - C. Faloutsos2 Constraints: eg., E-R Model: Key cardinalities of a Relationship

3 Carnegie Mellon 15-415 - C. Faloutsos3 Overview Domain; Ref. Integrity constraints Assertions and Triggers Functional dependencies

4 Carnegie Mellon 15-415 - C. Faloutsos4 Domain Constraints Domain Types, eg, SQL –Fixed Length characters –Int; float; (date) null values eg., create table student( ssn char(9) not null,...)

5 Carnegie Mellon 15-415 - C. Faloutsos5 Referential Integrity constraints ‘foreign keys’ - eg: create table takes( ssn char(9) not null, c-id char(5) not null, grade integer, primary key(ssn, c-id), foreign key ssn references student, foreign key c-id references class)

6 Carnegie Mellon 15-415 - C. Faloutsos6 Referential Integrity constraints … foreign key ssn references student, foreign key c-id references class) Effect: –expects that ssn to exist in ‘student’ table –blocks ops that violate that - how?? insertion? deletion/update?

7 Carnegie Mellon 15-415 - C. Faloutsos7 Referential Integrity constraints … foreign key ssn references student on delete cascade on update cascade,... -> eliminate all student enrollments other options (set to null, to default etc)

8 Carnegie Mellon 15-415 - C. Faloutsos8 Weapons for IC: assertions –create assertion check triggers (~ assertions with ‘teeth’) –on operation, if condition, then action

9 Carnegie Mellon 15-415 - C. Faloutsos9 Triggers - example define trigger zerograde on update takes (if new takes.grade < 0 then takes.grade = 0)

10 Carnegie Mellon 15-415 - C. Faloutsos10 Triggers - discussion more complicated: “managers have higher salaries than their subordinates” - a trigger can automatically boost mgrs salaries triggers: tricky (infinite loops…)

11 Carnegie Mellon 15-415 - C. Faloutsos11 Overview Domain; Ref. Integrity constraints Assertions and Triggers Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

12 Carnegie Mellon 15-415 - C. Faloutsos12 Functional dependencies motivation: ‘good’ tables takes1 (ssn, c-id, grade, name, address) ‘good’ or ‘bad’?

13 Carnegie Mellon 15-415 - C. Faloutsos13 Functional dependencies takes1 (ssn, c-id, grade, name, address)

14 Carnegie Mellon 15-415 - C. Faloutsos14 Functional dependencies ‘Bad’ - why?

15 Carnegie Mellon 15-415 - C. Faloutsos15 Functional Dependencies Redundancy –space –inconsistencies –insertion/deletion anomalies (later…) What caused the problem?

16 Carnegie Mellon 15-415 - C. Faloutsos16 Functional dependencies ‘name’ depends on the ‘ssn’ define ‘depends’

17 Carnegie Mellon 15-415 - C. Faloutsos17 Overview Domain; Ref. Integrity constraints Assertions and Triggers Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

18 Carnegie Mellon 15-415 - C. Faloutsos18 Functional dependencies Definition: ‘a’ functionally determines ‘b’

19 Carnegie Mellon 15-415 - C. Faloutsos19 Functional dependencies Informally: ‘if you know ‘a’, there is only one ‘b’ to match’

20 Carnegie Mellon 15-415 - C. Faloutsos20 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)

21 Carnegie Mellon 15-415 - C. Faloutsos21 Functional dependencies ‘X’, ‘Y’ can be sets of attributes other examples??

22 Carnegie Mellon 15-415 - C. Faloutsos22 Functional dependencies ssn -> name, address ssn, c-id -> grade

23 Carnegie Mellon 15-415 - C. Faloutsos23 Overview Domain; Ref. Integrity constraints Assertions and Triggers Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

24 Carnegie Mellon 15-415 - C. Faloutsos24 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

25 Carnegie Mellon 15-415 - C. Faloutsos25 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?

26 Carnegie Mellon 15-415 - C. Faloutsos26 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

27 Carnegie Mellon 15-415 - C. Faloutsos27 FDs - Armstrong’s axioms Transitivity ssn->address address-> county-tax-rate THEN: ssn-> county-tax-rate

28 Carnegie Mellon 15-415 - C. Faloutsos28 FDs - Armstrong’s axioms Reflexivity: Augmentation: Transitivity: ‘sound’ and ‘complete’

29 Carnegie Mellon 15-415 - C. Faloutsos29 FDs - Armstrong’s axioms Additional rules: Union Decomposition Pseudo-transitivity

30 Carnegie Mellon 15-415 - C. Faloutsos30 FDs - Armstrong’s axioms Prove ‘Union’ from three axioms:

31 Carnegie Mellon 15-415 - C. Faloutsos31 FDs - Armstrong’s axioms Prove ‘Union’ from three axioms:

32 Carnegie Mellon 15-415 - C. Faloutsos32 FDs - Armstrong’s axioms Prove Pseudo-transitivity:

33 Carnegie Mellon 15-415 - C. Faloutsos33 FDs - Armstrong’s axioms Prove Decomposition

34 Carnegie Mellon 15-415 - C. Faloutsos34 Overview Domain; Ref. Integrity constraints Assertions and Triggers Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover

35 Carnegie Mellon 15-415 - C. Faloutsos35 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

36 Carnegie Mellon 15-415 - C. Faloutsos36 FDs - Closure F+ ssn, c-id -> grade ssn-> name, address ssn-> ssn ssn, c-id-> address c-id, address-> c-id... F+

37 Carnegie Mellon 15-415 - C. Faloutsos37 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

38 Carnegie Mellon 15-415 - C. Faloutsos38 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {ssn}+ ={ssn, name, address } }F}F

39 Carnegie Mellon 15-415 - C. Faloutsos39 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {c-id}+ = ?? }F}F

40 Carnegie Mellon 15-415 - C. Faloutsos40 FDs - Closure A+ takes(ssn, c-id, grade, name, address) ssn, c-id -> grade ssn-> name, address {c-id, ssn}+ = ?? }F}F

41 Carnegie Mellon 15-415 - C. Faloutsos41 FDs - Closure A+ if A+ = {all attributes of table} then ‘A’ is a candidate key

42 Carnegie Mellon 15-415 - C. Faloutsos42 FDs - A+ closure - not in book Diagrams AB->C (1) A->BC (2) B->C (3) A->B (4) C A B

43 Carnegie Mellon 15-415 - C. Faloutsos43 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

44 Carnegie Mellon 15-415 - C. Faloutsos44 FDs - ‘canonical cover’ Fc ssn, c-id -> grade ssn-> name, address ssn,name-> name, address ssn, c-id-> grade, name F Fc

45 Carnegie Mellon 15-415 - C. Faloutsos45 FDs - ‘canonical cover’ Fc why do we need it? define it properly compute it efficiently

46 Carnegie Mellon 15-415 - C. Faloutsos46 FDs - ‘canonical cover’ Fc why do we need it? –easier to compute candidate keys define it properly compute it efficiently

47 Carnegie Mellon 15-415 - C. Faloutsos47 FDs - ‘canonical cover’ Fc define it properly - three properties –every FD a->b has no extraneous attributes on the RHS –ditto for the LHS –all LHS parts are unique

48 Carnegie Mellon 15-415 - C. Faloutsos48 ‘extraneous’ attribute: – 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

49 Carnegie Mellon 15-415 - C. Faloutsos49 FDs - ‘canonical cover’ Fc ssn, c-id -> grade ssn-> name, address ssn,name-> name, address ssn, c-id-> grade, name F

50 Carnegie Mellon 15-415 - C. Faloutsos50 FDs - ‘canonical cover’ Fc Algorithm: examine each FD; drop extraneous LHS or RHS attributes merge FDs with same LHS repeat until no change

51 Carnegie Mellon 15-415 - C. Faloutsos51 FDs - ‘canonical cover’ Fc Trace algo for AB->C (1) A->BC (2) B->C (3) A->B (4)

52 Carnegie Mellon 15-415 - C. Faloutsos52 FDs - ‘canonical cover’ Fc Trace algo for AB->C (1) A->BC (2) B->C (3) A->B (4) (4) and (2) merge: AB->C (1) A->BC (2) B->C (3)

53 Carnegie Mellon 15-415 - C. Faloutsos53 FDs - ‘canonical cover’ Fc AB->C (1) A->BC (2) B->C (3) in (2): ‘C’ is extr. AB->C (1) A->B (2’) B->C (3)

54 Carnegie Mellon 15-415 - C. Faloutsos54 FDs - ‘canonical cover’ Fc AB->C (1) A->B (2’) B->C (3) in (1): ‘A’ is extr. B->C (1’) A->B (2’) B->C (3)

55 Carnegie Mellon 15-415 - C. Faloutsos55 FDs - ‘canonical cover’ Fc B->C (1’) A->B (2’) B->C (3) (1’) and (3) merge A->B (2’) B->C (3) nothing is extraneous: ‘canonical cover’

56 Carnegie Mellon 15-415 - C. Faloutsos56 FDs - ‘canonical cover’ Fc AFTER A->B (2’) B->C (3) BEFORE AB->C (1) A->BC (2) B->C (3) A->B (4)

57 Carnegie Mellon 15-415 - C. Faloutsos57 Overview - conclusions Domain; Ref. Integrity constraints Assertions and Triggers Functional dependencies –why –definition –Armstrong’s “axioms” –closure and cover


Download ppt "Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Integrity Constraints."

Similar presentations


Ads by Google