Assertions and Triggers
Assertions and Triggers
Database Systems
(CSF212)
by
Dr. Shubhangi
Dept. of CS and IS
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,branch
where loan.branch-name =
branch.branch-name)
>= (select sum(balance) from account,branch
where account.branch-name =
branch.branch-name)))
Assertion Example
Every loan has at least one borrower who maintains an account with a
minimum balance of $1000.00
create assertion balance-constraint check
(not exists (
select * from loan
where not exists (
select *
from borrower, depositor, account,loan
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)))
Assertion Example
The salary of the employee must not be
greater than the salary of the manager of
the department for which the employee
work for.
Drop an assertion
To drop an assertion, you can use the following statement:
DROP ASSERTION assertion_name;
When the event happens, the system will check the constraint, and
if satisfied, will perform the action.
NOTE: triggers may cause cascading effects.
Triggers not part of SQL2 but included in SQL3… however,
database vendors did not wait for standards with triggers!
Elements of Triggers (in SQL3)
• Timing of action execution
• before
• after
• instead of
…. the triggering event
• The action can refer to both the old and new state of the database.
“Whenever we insert employees tuples, make sure that their dno’s exist in Dept.”