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

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

Embed Size (px)

Citation preview

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

Carnegie Mellon

Carnegie Mellon Univ.Dept. of Computer Science

15-415 - Database Applications

C. Faloutsos

Integrity Constraints

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

15-415 - C. Faloutsos 2Carnegie Mellon

Constraints:

eg., E-R Model:

• Key

• cardinalities of a Relationship

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

15-415 - C. Faloutsos 3Carnegie Mellon

Overview

• Domain; Ref. Integrity constraints

• Assertions and Triggers

• Functional dependencies

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

15-415 - C. Faloutsos 4Carnegie Mellon

Domain Constraints

• Domain Types, eg, SQL– Fixed Length characters– Int; float; (date)

• null values eg.,create table student( ssn char(9) not null, ...)

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

15-415 - C. Faloutsos 5Carnegie Mellon

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)

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

15-415 - C. Faloutsos 6Carnegie Mellon

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?

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

15-415 - C. Faloutsos 7Carnegie Mellon

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)

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

15-415 - C. Faloutsos 8Carnegie Mellon

Weapons for IC:

• assertions– create assertion <assertion-name> check

<predicate>

• triggers (~ assertions with ‘teeth’)– on operation, if condition, then action

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

15-415 - C. Faloutsos 9Carnegie Mellon

Triggers - example

define trigger zerograde on update takes

(if new takes.grade < 0

then takes.grade = 0)

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

15-415 - C. Faloutsos 10Carnegie Mellon

Triggers - discussion

• more complicated: “managers have higher salaries than their subordinates” - a trigger can automatically boost mgrs salaries

• triggers: tricky (infinite loops…)

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

15-415 - C. Faloutsos 11Carnegie Mellon

Overview

• Domain; Ref. Integrity constraints

• Assertions and Triggers

• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover

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

15-415 - C. Faloutsos 12Carnegie Mellon

Functional dependencies

• motivation: ‘good’ tables

takes1 (ssn, c-id, grade, name, address)

‘good’ or ‘bad’?

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

15-415 - C. Faloutsos 13Carnegie Mellon

Functional dependencies

takes1 (ssn, c-id, grade, name, address)

Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

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

15-415 - C. Faloutsos 14Carnegie Mellon

Functional dependencies

‘Bad’ - why?

Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

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

15-415 - C. Faloutsos 15Carnegie Mellon

Functional Dependencies

• Redundancy– space– inconsistencies– insertion/deletion anomalies (later…)

• What caused the problem?

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

15-415 - C. Faloutsos 16Carnegie Mellon

Functional dependencies

• ‘name’ depends on the ‘ssn’

• define ‘depends’Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

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

15-415 - C. Faloutsos 17Carnegie Mellon

Overview

• Domain; Ref. Integrity constraints

• Assertions and Triggers

• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover

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

15-415 - C. Faloutsos 18Carnegie Mellon

Functional dependencies

Definition:

‘a’ functionally determines ‘b’Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

ba

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

15-415 - C. Faloutsos 19Carnegie Mellon

Functional dependencies

Informally: ‘if you know ‘a’, there is only one ‘b’ to match’

Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

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

15-415 - C. Faloutsos 20Carnegie Mellon

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)

])[2][1][2][1( ytytxtxtYX

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

15-415 - C. Faloutsos 21Carnegie Mellon

Functional dependencies

• ‘X’, ‘Y’ can be sets of attributes

• other examples??

Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

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

15-415 - C. Faloutsos 22Carnegie Mellon

Functional dependencies

• ssn -> name, address

• ssn, c-id -> grade

Ssn c-id Grade Name Address

123 413 A smith Main

123 415 B smith Main

123 211 A smith Main

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

15-415 - C. Faloutsos 23Carnegie Mellon

Overview

• Domain; Ref. Integrity constraints

• Assertions and Triggers

• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover

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

15-415 - C. Faloutsos 24Carnegie Mellon

Functional dependencies

Closure of a set of FD: all implied FDs - eg.:ssn -> name, address

ssn, c-id -> grade

implyssn, c-id -> grade, name, address

ssn, c-id -> ssn

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

15-415 - C. Faloutsos 25Carnegie Mellon

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?

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

15-415 - C. Faloutsos 26Carnegie Mellon

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

YXXY

YWXWYX

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

15-415 - C. Faloutsos 27Carnegie Mellon

FDs - Armstrong’s axioms

• Transitivity

ssn->address

address-> county-tax-rate

THEN:

ssn-> county-tax-rate

ZXZY

YX

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

15-415 - C. Faloutsos 28Carnegie Mellon

FDs - Armstrong’s axioms

Reflexivity:

Augmentation:

Transitivity:

ZXZY

YX

YXXY

YWXWYX

‘sound’ and ‘complete’

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

15-415 - C. Faloutsos 29Carnegie Mellon

FDs - Armstrong’s axioms

Additional rules:

• Union

• Decomposition

• Pseudo-transitivity

ZXWZYW

YX

ZX

YXYZX

YZXZX

YX

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

15-415 - C. Faloutsos 30Carnegie Mellon

FDs - Armstrong’s axioms

Prove ‘Union’ from three axioms:

YZXZX

YX

?

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

15-415 - C. Faloutsos 31Carnegie Mellon

FDs - Armstrong’s axioms

Prove ‘Union’ from three axioms:

YZXtytransitiviand

thusXisXXbut

XZXXXwaugm

YZXZZwaugm

ZX

YX

)4()3(

;

)4(/.)2(

)3(/.)1(

)2(

)1(

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

15-415 - C. Faloutsos 32Carnegie Mellon

FDs - Armstrong’s axioms

Prove Pseudo-transitivity:

ZXWZYW

YX

?

ZXZY

YX

YXXY

YWXWYX

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

15-415 - C. Faloutsos 33Carnegie Mellon

FDs - Armstrong’s axioms

Prove Decomposition

ZXZY

YX

YXXY

YWXWYX

ZX

YXYZX

?

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

15-415 - C. Faloutsos 34Carnegie Mellon

Overview

• Domain; Ref. Integrity constraints

• Assertions and Triggers

• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover

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

15-415 - C. Faloutsos 35Carnegie Mellon

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

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

15-415 - C. Faloutsos 36Carnegie Mellon

FDs - Closure F+

ssn, c-id -> grade

ssn-> name, address

ssn-> ssn

ssn, c-id-> address

c-id, address-> c-id

...

F+

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

15-415 - C. Faloutsos 37Carnegie Mellon

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

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

15-415 - C. Faloutsos 38Carnegie Mellon

FDs - Closure A+

takes(ssn, c-id, grade, name, address)

ssn, c-id -> grade

ssn-> name, address

{ssn}+ ={ssn,

name, address }

}F

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

15-415 - C. Faloutsos 39Carnegie Mellon

FDs - Closure A+

takes(ssn, c-id, grade, name, address)

ssn, c-id -> grade

ssn-> name, address

{c-id}+ = ??

}F

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

15-415 - C. Faloutsos 40Carnegie Mellon

FDs - Closure A+

takes(ssn, c-id, grade, name, address)

ssn, c-id -> grade

ssn-> name, address

{c-id, ssn}+ = ??

}F

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

15-415 - C. Faloutsos 41Carnegie Mellon

FDs - Closure A+

if A+ = {all attributes of table}

then ‘A’ is a candidate key

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

15-415 - C. Faloutsos 42Carnegie Mellon

FDs - A+ closure - not in book

Diagrams

AB->C (1)

A->BC (2)

B->C (3)

A->B (4)

CA

B

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

15-415 - C. Faloutsos 43Carnegie Mellon

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

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

15-415 - C. Faloutsos 44Carnegie Mellon

FDs - ‘canonical cover’ Fc

ssn, c-id -> grade

ssn-> name, address

ssn,name-> name, address

ssn, c-id-> grade, name

F

Fc

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

15-415 - C. Faloutsos 45Carnegie Mellon

FDs - ‘canonical cover’ Fc

• why do we need it?

• define it properly

• compute it efficiently

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

15-415 - C. Faloutsos 46Carnegie Mellon

FDs - ‘canonical cover’ Fc

• why do we need it?– easier to compute candidate keys

• define it properly

• compute it efficiently

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

15-415 - C. Faloutsos 47Carnegie Mellon

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

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

15-415 - C. Faloutsos 48Carnegie Mellon

‘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

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

15-415 - C. Faloutsos 49Carnegie Mellon

FDs - ‘canonical cover’ Fc

ssn, c-id -> grade

ssn-> name, address

ssn,name-> name, address

ssn, c-id-> grade, name

F

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

15-415 - C. Faloutsos 50Carnegie Mellon

FDs - ‘canonical cover’ Fc

Algorithm:

• examine each FD; drop extraneous LHS or RHS attributes

• merge FDs with same LHS

• repeat until no change

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

15-415 - C. Faloutsos 51Carnegie Mellon

FDs - ‘canonical cover’ Fc

Trace algo for

AB->C (1)

A->BC (2)

B->C (3)

A->B (4)

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

15-415 - C. Faloutsos 52Carnegie Mellon

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)

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

15-415 - C. Faloutsos 53Carnegie Mellon

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)

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

15-415 - C. Faloutsos 54Carnegie Mellon

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)

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

15-415 - C. Faloutsos 55Carnegie Mellon

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’

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

15-415 - C. Faloutsos 56Carnegie Mellon

FDs - ‘canonical cover’ Fc

AFTER

A->B (2’)

B->C (3)

BEFORE

AB->C (1)

A->BC (2)

B->C (3)

A->B (4)

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

15-415 - C. Faloutsos 57Carnegie Mellon

Overview - conclusions

• Domain; Ref. Integrity constraints

• Assertions and Triggers

• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover