Professional Documents
Culture Documents
Lecture Constraint COMP232
Lecture Constraint COMP232
Constraint
• Domain Constraints
• Referential Integrity
• Assertions
• Triggers
• Functional Dependencies
Domain Constraints
R1 ( K1, …, … ) R2 ( K2, …, , … )
t2
x
t1
x
Referential Integrity for Insertion and Deletion
• The following tests must be made in order to preserve the
following referential integrity constraint:
(r2) K(r1)
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,
or the update may be applied to the tuples in the set (cascade
update), or the tuples in the set may be deleted.
Referential Integrity in SQL
• The sum of all loan amounts for each branch must be less than the
sum of all account balances at the branch.
must return T or F
Triggers
• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run) [Optional]
– Action (what happens if the trigger runs)
• Semantics:
– When event occurs, and condition is satisfied, the action is
performed.
Triggers – Event,Condition,Action
• Events could be :
BEGIN
END;
Example Trigger
BEGIN
END;
Example Trigger
BEGIN
END;
Example trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
FOR EACH ROW
BEGIN
IF (:new.salary < 60000)
THEN RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END IF;
END;
.
run;
Details of Trigger Example
BEGIN
RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END;
.
run;
Example Trigger
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
Use of Functional Dependencies
– 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
Closure of Attribute Sets
• Define the closure of under F (denoted by +) as the set of
attributes that are functionally determined by under F:
is in F+ +
is a set of attributes
Given loan-no
If loan-no amount
then amount is part of loan-no+
I.e., loan-no+= loan-no,amount, …
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)
result contains all of
4. Result=ABCGHI (CG I and CG
the attributes of R,
AGBCH)
so stop
• Is AG a candidate key?
1. AG R Question: What is A+ and G+ ?
2. Does A+ R?
3. Does G+ R?
Canonical Cover
• Consider a set F of functional dependencies and the functional
dependency in F.
– Attribute A is extraneous in if A and if A is removed from , the set
of functional dependencies implied by F doesn‟t change. From A C
Given AB C and A C then B is extraneous in AB I get AB C
– Attribute A is extraneous in if A and if A is removed from , the set
of functional dependencies implied by F doesn‟t change.
Given A BC and A B then B is extraneous in BC
• A canonical cover Fc for F is a set of dependencies such that F logically
implies all dependencies in Fc and Fc logically implies all dependencies
in F, and further
– No functional dependency in Fc contains an extraneous attribute.
– Each left side of a functional dependency in Fc is unique.
Canonical Cover
• 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
Example
• R = (A, B, C, G, H, I)
F={ AB
AC
CG H
CG I
B H}
• some members of F +
– AH
• by transitivity from A B and B H
– AG I
• by augmenting A C with G, to get AG CG
and then transitivity with CG I
– CG HI
• from CG H and CG I : “union rule” can be inferred from
– definition of functional dependencies, or
– Augmentation of CG I to infer CG CGI, augmentation of
CG H to infer CGI HI, and then transitivity