0% found this document useful (0 votes)
48 views17 pages

13.2 - Chapter 7 - Triggers

1. A trigger is a procedure that is invoked by the DBMS in response to data changes, such as inserts, updates or deletes, on a particular table. 2. Triggers can be used to implement business rules, ensure data integrity, and maintain related data across multiple tables. 3. A trigger has an event that activates it, an optional condition, and an action. The action contains SQL statements that are executed if the condition is met in response to the event.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views17 pages

13.2 - Chapter 7 - Triggers

1. A trigger is a procedure that is invoked by the DBMS in response to data changes, such as inserts, updates or deletes, on a particular table. 2. Triggers can be used to implement business rules, ensure data integrity, and maintain related data across multiple tables. 3. A trigger has an event that activates it, an optional condition, and an action. The action contains SQL statements that are executed if the condition is met in response to the event.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

ĐẠI HỌC FPT CẦN THƠ

Chapter 7 – Triggers
Objectives

1 Know what is a trigger

2 Know why use triggers

3 Know components of triggers

4 Know how to create a trigger


Contents

1 Trigger definition

2 Why use triggers?

3 Components of trigger

4 Trigger Guideline
1. Trigger definition

A trigger is a procedure that is invoked by the


DBMS as a response to a special change
(insert, delete, update to a particular relation
or transaction end)
Triggers are available in most current
commercial DB products
Trigger carry out actions when their triggering
conditions are met
Why use triggers?

Triggers can implement business rules


E.g. creating a new loan when a customer’s account is
overdrawn.
Triggers be used to ensure data integrity
Triggers may also be used to maintain data in
related database tables.
E.g. Updating derived attributes when underlying data is
changed, or maintaining summary data.
Trigger components

Event (activates the trigger)


A specified modification to the DB
May be an insert, deletion or change.
May be limited to special tables.
The trigger may fire before or after the transaction
Condition (test whether the triggers should run)
A Boolean expression or a query
If the query answer set is non-empty it evaluates to true,
otherwise false.
If the condition is true the trigger action occurs
Trigger components

Action (what happens if the trigger runs)


A trigger’s action can be very far-ranging, e.g.
Execute queries.
Make modifications to the DB.
Create new tables.
Call procedures
Trigger components
Triggers sometimes called event-conditon-action rules
or ECA rules
Triggers differ from the kinds of constraints in three
ways:
Triggers are only awakened when certain events (insert,
delete, or update to a particular relation)
Once awakened by its triggering event, the trigger tests a
condition. If the condition does not hold, then nothing else
associated with the trigger happens in response to this event
If the condition of the trigger is satisfied , the action
associated with the trigger is performed by the DBMS.
Trigger guideline
The key elements and the order in the syntax for
triggers:
1. The CREATE TRIGGER statement
2. The clause indicating the trigger event and telling
whether the trigger uses the database state before or
after the triggering event.
3. A REFERENCING clause to allow the condition and action
of the trigger to refer to the tuple being modified.
4. A clause telling whether trigger executes once for each
modified row or once for all the modifications made by
one SQL statement.
5. The condition, which uses the keyword WHEN and a
boolean expression.
6. The action, consisting of one or more SQL statements.
Trigger guideline

CREATE TRIGGER trigger_name  


ON { relation_name }   
[ WITH <Options> ]  
{ FOR | AFTER | INSTEAD OF }   
{ [INSERT], [UPDATE] , [DELETE] }
AS
BEGIN
sql_statements
END
Trigger guideline

Create a trigger when updating or


inserting data of the TONKHO
table, the SLCuoi column is
calculated by the formula SLCuoi
= SLDau + TongSLN – TongSLX
Trigger guideline

CREATE TRIGGER triggertonkho_insert


ON tonkho IF (@slc_i <> (@sld_i + @tsln_i - @tslx_i))
FOR INSERT, UPDATE BEGIN
AS RAISERROR ('Error: Wrong numbers', 16,1)
BEGIN ROLLBACK TRANSACTION;
END
DECLARE @sld_i int;
END;
DECLARE @slc_i int;
DECLARE @tsln_i int;
DECLARE @tslx_i int;
SELECT @sld_i = INSERTED.slDau,
@slc_i = INSERTED.slCuoi,
@tsln_i = INSERTED.TongSLN,
@tslx_i = INSERTED.TongSLX
FROM INSERTED;
Trigger guideline

TESTING:
INSERT INTO TONKHO VALUES ('200701','DD03','13','10','12','10')
The options for trigger design

Synchronization of the trigger with the activating


statement
BEFORE
AFTER
Possible triggering events are:
UPDATE
INSERT
DELETE
An OF clause is not permitted for INSERT and DELETE events; these
events make sense for entire tuples only
The options for trigger design

There are two kinds of triggers


Statement-level trigger: executed once for all the tuples are
changed in one SQL statement.
REFERENCING NEW TABLE AS newtuples,
OLD TABLE AS oldtuples
Row-level trigger: executed once for each modified tuple.
REFERENCING OLD AS oldtuple,
NEW AS newtuple,
Newtuples, oldtuples, oldtuple, newtuple can be used in the
CONDITION and ACTION clause
The options for trigger design

Number of Activations of the trigger


Once per modified tuple: FOR EACH ROW
Once per activating statement (default).
WHEN clause is optional.
The action of trigger can consist of multiple SQL
statements, surrounded by BEGIN… END.
ĐẠI HỌC FPT CẦN THƠ

You might also like