chap6 Integrity constraint
chap6 Integrity constraint
$
• Domain Constraints
• Referential Integrity
• Assertions
• Triggers
• Functional Dependencies
&
Database Systems Concepts 6.1 Silberschatz, Korth and Sudarshan c 1997
%
' Domain Constraints
$
• Integrity constraints guard against accidental damage to the
database, by ensuring that authorized changes to the
database do not result in a loss of data consistency.
&
Database Systems Concepts 6.2 Silberschatz, Korth and Sudarshan c 1997
%
' Domain Constraints (Cont.)
$
• The check clause in SQL-92 permits domains to be restricted:
– Use check clause to ensure that an hourly-wage domain
allows only values greater than a specified value.
create domain hourly-wage numeric(5,2)
constraint value-test check(value >= 4.00)
– The domain hourly-wage is declared to be a decimal
number with 5 digits, 2 of which are after the decimal point
– The domain has a constraint that ensures that the
& %
hourly-wage is greater than 4.00.
– The clause constraint value-test is optional; useful to
indicate which constraint an update violated.
& %
– The subset α of R2 is a foreign key referencing K1 in
relation r1 , if for every t2 in r2 there must be a tuple t1 in r1
such that t1 [K1 ] = t2 [α].
– Referential integrity constraint: Πα (r2 ) ⊆ ΠK1 (r1 )
&
Database Systems Concepts 6.5 Silberschatz, Korth and Sudarshan c 1997
%
' Database Modification
$
• The following tests must be made in order to preserve the
following referential integrity constraint:
Πα (r2 ) ⊆ ΠK (r1 )
t2 [α] ∈ ΠK (r1 )
& %
σα = t1 [K ] (r2 )
& %
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 cascaded to the tuples in
the set, or the tuples in the set may be deleted.
& %
– 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.
& %
assets integer,
primary key (branch-name))
& %
account-number char(10) not null,
primary key (customer-name, account-number),
foreign key (account-number) references account,
foreign key (customer-name) references customer )
& %
delete “cascades” to the account relation, deleting the tuple
that refers to the branch that was deleted.
& %
undone.
&
Database Systems Concepts 6.13 Silberschatz, Korth and Sudarshan c 1997
%
' Assertion Example
$
• The sum of all loan amounts for each branch must be less than
the sum of all account balances at the branch.
&
Database Systems Concepts 6.14 Silberschatz, Korth and Sudarshan c 1997
%
' Assertion Example
$
• Every loan has at least one borrower who maintains an
account with a minimum balance of $1000.00.
& %
and depositor.account-number = account.account-number
and account.balance >= 1000)))
& %
implementations support triggers.
&
Database Systems Concepts 6.17 Silberschatz, Korth and Sudarshan c 1997
%
' Trigger Example (Cont.)
$
define trigger overdraft on update of account T
(if new T.balance < 0
then (insert into loan values
(T.branch-name, T.account-number , − new T.balance)
insert into borrower
(select customer-name, account-number
from depositor
where T.account-number = depositor.account-number )
update account S
set S.balance = 0
& %
where S.account-number = T.account-number ))
The keyword new used before T.balance indicates that the value of
T.balance after the update should be used; if it is omitted, the value
before the update is used.
&
Database Systems Concepts 6.19 Silberschatz, Korth and Sudarshan c 1997
%
' Functional Dependencies (Cont.)
$
• Let R be a relation schema
α ⊆ R, β ⊆ R
& %
• K is a superkey for relation schema R if and only if K → R
• K is a candidate key for R if and only if
– K → R , and
– for no α ⊂ K , α → R
& %
but would not expect the following to hold:
loan-number → customer-name
& %
functional dependency even if the functional dependency does
not hold on all legal instances. For example, a specific
instance of Loan-schema may, by chance, satisfy loan-number
→ customer-name.
& %
– if α → β, then γα → γβ (augmentation)
– if α → β and β → γ, then α → γ (transitivity)
These rules are sound and complete.
&
Database Systems Concepts 6.24 Silberschatz, Korth and Sudarshan c 1997
%
' Example
$
• R = (A, B, C, G, H, I )
• F = {A → B
A → C
CG → H
CG → I
B → H}
• some members of F +
& %
– A → H
– AG → I
– CG → HI
α → β is in F + ⇔ β ⊆ α+
result := α;
while (changes to result) do
for each β → γ in F do
& %
begin
if β ⊆ result then result := result ∪ γ;
end
& %
4. result = ABCGHI
• Is AG a candidate key?
1. AG → R
2. does A+ → R ?
3. does G+ → R ?
& %
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.
&
Database Systems Concepts 6.29 Silberschatz, Korth and Sudarshan c 1997
%
' Example of Computing a Canonical Cover
$
• R = (A, B, C )
F = {A → BC
B → C
A → B
AB → C }
• Combine A → BC and A → B into A → BC
• A is extraneous in AB → C because B → C logically implies
AB → C .
• C is extraneous in A → BC since A → BC is logically implied by
& %
A → B and B → C.
• The canonical cover is:
A→B
B→C