Chap 6
Chap 6
Comp 231
231 Database
Database Management
Management Systems
Systems
6. Integrity Constraints
• Domain Constraints
• Referential Integrity
• Assertions
• Triggers
• Functional Dependencies
Foreign key
R1 ( K1, …, … ) R2 ( K2, …, , … )
t2
x
t1
x
works-
Works-for ( employee-no, dept-no) Primary key
Emp for Dep of Works-for
Foreign keys
using the old value of t1 (the value before the update is applied).
If this set is not empty, the update may be rejected as an error, o
r the update may be applied to the tuples in the set (cascade upd
ate), or the tuples in the set may be deleted.
Department of Computer Science and Engineering, HKUST
Slide 10
Referential
Referential Integrity
Integrity in
in SQL
SQL
• Primary and candidate keys and foreign keys can be specified as
part of the SQL create table statement:
– The primary key clause of the create table statement
includes a list of the attributes that comprise the primary
key.
– The unique key clause of the create table statement includes
a list of the attributes that comprise a candidate key.
– The foreign key clause of the create table statement
includes both a list of the attributes that comprise the
foreign key and the name of the relation referenced by the
foreign key.
loan-info
Another example:
branch-nm loan-no cust-nm amount
reverse of the fd’s above Perryridge L-001 Peter Yeung 100000
Perryridge L-001 Peter Yeung 100000
Central L-001 Peter Yeung 250000
Wanchai L-002 Leon Lai 100000
– if , then (reflexivity)
– if , then (augmentation)
– if and , then (transitivity)
– if , then (augmentation)
loan-no amount (given)
loan-no, branch-name amount, branch-name
• R = (A, B, C, G, H, I)
• F = {A B
A C
CG H
CG I
B H}
• some members of F+
A H A B; B H
AG I A C; AG CG; CG I
CG HI
If loan-no branch-name
then branch-name is part of loan-no+
I.e., loan-no+= loan-no,amount, branch-name …
• R = (A, B, C, G, H, I)
F= ( A B
A C
CG H
CG I
B H}
• (AG+)
1. Result= AG
2. Result= ABCG (A C; A B and A
AG)
3. Result= ABCGH (CG H and CG AGBC)
4. Result=ABCGHI (CG I and CG AGBCH)
• Is AG a candidate key? result contains all of
1. AG R the attributes of R,
2. Does A+ R? so stop
3. Does G+ R?
Question: What is A+ and G+ ?
• R = (A, B, C)
F={ A BC
A BC
B C
B C
A B
AB C
AB C}
• Combine A BC and A B into A
A BC
BC A BC
B C
• A is extraneous in AB C because B B C B C
C logically implies AB C.
• C is extraneous in A BC since A BC A B
is logically implied by A B and B C. B C
• The canonical cover is:
AB
BC