carnegie mellon carnegie mellon univ. dept. of computer science 15-415 - database applications c....
TRANSCRIPT
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
15-415 - C. Faloutsos 3Carnegie Mellon
Overview
• Domain; Ref. Integrity constraints
• Assertions and Triggers
• Functional dependencies
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, ...)
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)
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?
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)
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
15-415 - C. Faloutsos 9Carnegie Mellon
Triggers - example
define trigger zerograde on update takes
(if new takes.grade < 0
then takes.grade = 0)
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…)
15-415 - C. Faloutsos 11Carnegie Mellon
Overview
• Domain; Ref. Integrity constraints
• Assertions and Triggers
• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover
15-415 - C. Faloutsos 12Carnegie Mellon
Functional dependencies
• motivation: ‘good’ tables
takes1 (ssn, c-id, grade, name, address)
‘good’ or ‘bad’?
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
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
15-415 - C. Faloutsos 15Carnegie Mellon
Functional Dependencies
• Redundancy– space– inconsistencies– insertion/deletion anomalies (later…)
• What caused the problem?
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
15-415 - C. Faloutsos 17Carnegie Mellon
Overview
• Domain; Ref. Integrity constraints
• Assertions and Triggers
• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover
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
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
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
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
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
15-415 - C. Faloutsos 23Carnegie Mellon
Overview
• Domain; Ref. Integrity constraints
• Assertions and Triggers
• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover
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
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?
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
15-415 - C. Faloutsos 27Carnegie Mellon
FDs - Armstrong’s axioms
• Transitivity
ssn->address
address-> county-tax-rate
THEN:
ssn-> county-tax-rate
ZXZY
YX
15-415 - C. Faloutsos 28Carnegie Mellon
FDs - Armstrong’s axioms
Reflexivity:
Augmentation:
Transitivity:
ZXZY
YX
YXXY
YWXWYX
‘sound’ and ‘complete’
15-415 - C. Faloutsos 29Carnegie Mellon
FDs - Armstrong’s axioms
Additional rules:
• Union
• Decomposition
• Pseudo-transitivity
ZXWZYW
YX
ZX
YXYZX
YZXZX
YX
15-415 - C. Faloutsos 30Carnegie Mellon
FDs - Armstrong’s axioms
Prove ‘Union’ from three axioms:
YZXZX
YX
?
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(
15-415 - C. Faloutsos 32Carnegie Mellon
FDs - Armstrong’s axioms
Prove Pseudo-transitivity:
ZXWZYW
YX
?
ZXZY
YX
YXXY
YWXWYX
15-415 - C. Faloutsos 33Carnegie Mellon
FDs - Armstrong’s axioms
Prove Decomposition
ZXZY
YX
YXXY
YWXWYX
ZX
YXYZX
?
15-415 - C. Faloutsos 34Carnegie Mellon
Overview
• Domain; Ref. Integrity constraints
• Assertions and Triggers
• Functional dependencies– why– definition– Armstrong’s “axioms”– closure and cover
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
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+
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
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
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
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
15-415 - C. Faloutsos 41Carnegie Mellon
FDs - Closure A+
if A+ = {all attributes of table}
then ‘A’ is a candidate key
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
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
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
15-415 - C. Faloutsos 45Carnegie Mellon
FDs - ‘canonical cover’ Fc
• why do we need it?
• define it properly
• compute it efficiently
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
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
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
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
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
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)
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)
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)
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)
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’
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)
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