Integrity Constraints
Integrity Constraints
Integrity Constraints
5.1 Introduction:
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
• Consider relationship set R between entity sets E1 and E2. The relational schema for
R includes the primary keys K1 of E1 and K2 of E2.
Then K1 and K2 form foreign keys on the relational schemas for E1 and E2
respectively that leads referential integrity constraints.
Example:
create table customer
(customer_name char(20),
customer_street char(30),
customer_city char(30),
primary key (customer_name ));
5.7 Assertions:
E.g. Every loan has at least one borrower who maintains an account with a minimum
balance or $1000.00
create assertion balance_constraint check
(not exists (
select *
from loan
where not exists (
select *
from borrower, depositor, account
where loan.loan_number = borrower.loan_number
and borrower.customer_name = depositor.customer_name
and depositor.account_number = account.account_number
and account.balance >= 1000)))
The sum of all loan amounts for each branch must be less than the sum of all account
balances at the branch.
create assertion sum_constraint check
(not exists (select *
from branch
where (select sum(amount )
from loan
where loan.branch_name =
branch.branch_name )
>= (select sum (amount )
from account
where loan.branch_name =
branch.branch_name )))
5.8 Triggers:
• In a DBMS, a trigger is a SQL procedure that initiates (fires) an action when an
event (INSERT, DELETE, or UPDATE) occurs. Since triggers are event-driven
specialized procedures, the DBMS stores and manages them. A trigger cannot be
called or executed; the DBMS automatically fires the trigger as a result of a data
modification to the associated table. Triggers maintain the referential integrity
• A trigger is a statement that is executed automatically by the system as a side
effect of a modification to the database.
• To design a trigger mechanism, we must:
– Specify the conditions under which the trigger is to be executed.
– Specify the actions to be taken when the trigger executes.
Triggers introduced to SQL standard in SQL:1999, but supported even earlier using non-
standard syntax by most databases.
• E.g Suppose that instead of allowing negative account balances, the bank deals with
overdrafts by
– setting the account balance to zero
– creating a loan in the amount of the overdraft