0% found this document useful (0 votes)
24 views12 pages

Assertions and Triggers

The document discusses assertions and triggers in database systems, explaining their purpose in maintaining data integrity and enforcing business rules. Assertions are predicates that ensure certain conditions are met in the database, while triggers allow for specific actions to be executed based on events and conditions. Examples of assertions and triggers are provided to illustrate their implementation in SQL.

Uploaded by

f20230371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Assertions and Triggers

The document discusses assertions and triggers in database systems, explaining their purpose in maintaining data integrity and enforcing business rules. Assertions are predicates that ensure certain conditions are met in the database, while triggers allow for specific actions to be executed based on events and conditions. Examples of assertions and triggers are provided to illustrate their implementation in SQL.

Uploaded by

f20230371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Database Systems
(CSF212)
by

Dr. Shubhangi
Dept. of CS and IS

BITS, PILANI – K. K. BIRLA GOA


3/4/2024 CAMPUS 1
Assertions
Assertions
 An assertion is a predicate expressing a condition that
we wish the database always to satisfy.
 An assertion in SQL takes the form
create assertion <assertion-name> check <predicate>
 When an assertion is made, the system tests it for
validity, and tests it again on every update that may
violate the assertion.
 This testing may introduce a significant amount of
overhead; hence assertions should be used with great
care.
 Asserting for all X, P(X) is achieved in a round-about
fashion using not exists X such that not P(X)
NOTE

 Inside the NOT EXISTS clause specify a


query that violates the required condition.

 Inner query result must be empty. If the


query result is not empty, the assertion
has been violated.
Assertion Example

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;

Assertions are useful for enforcing data integrity and


ensuring that the data in the database meets certain
conditions. They can be used to enforce business rules or
to ensure the consistency of the data.
Tiggers
 Enable the database programmer to specify:
• when to check a constraint,
• what exactly to do.

 A trigger has 3 parts:


• An event (e.g., update to an attribute)
• A condition (e.g., a query to check)
• An action (deletion, update, insertion)

 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.

• Update events may specify a particular column or set of columns.

• A condition is specified with a WHEN clause.

• The action can be performed either for


• once for every tuple, or
• once for all the tuples that are changed by the database operation.
Example: Row Level Trigger
CREATE TRIGGER NoLowerPrices

AFTER UPDATE OF price ON Product //price is an attribute and


Product is the table
REFERENCING
OLD AS OldTuple
NEW AS NewTuple
WHEN (OldTuple.price > NewTuple.price)
UPDATE Product
SET price = OldTuple.price
WHERE name = NewTuple.name

FOR EACH ROW


Statement Level Trigger
emp(dno…), dept(dept#, …)

“Whenever we insert employees tuples, make sure that their dno’s exist in Dept.”

CREATE TRIGGER deptExistTrig


AFTER INSERT ON emp
REFERENCING
OLD_TABLE AS OldStuff
NEW_TABLE AS NewStuff
WHEN (EXISTS (SELECT * FROM NewStuff
WHERE dno NOT IN
(SELECT dept# FROM dept)))
DELETE FROM NewStuff
WHERE dno NOT IN
(SELECT dept# FROM dept));

You might also like