Triggers Lecture
Triggers Lecture
1
2 What is a trigger?
A block of code that is attached to an event.
When that event occurs the trigger code is
fired.
A stored block with [Declare], Begin, End.
Associated with a database table or action
Fires automatically when certain DML action is
carried out on the table
Before or after an event
Change may be INSERT, DELETE, UPDATE
Do we want to perform on multiple rows?
If not, then Statement level trigger fires once only
If so, then Row level trigger fires for each affected
row
Trigger Uses
1. Auditing
3
Write information about (sensitive) data modifications to an audit
table
May include old and new values, user, timestamp
E.g. new and old salary
2. Data Integrity
Implement checks on data against business rules
Can compare with live database values
NEW and OLD values can be compared
E.g. prices must not go down
3. Referential integrity
Allows implementation of a "cascade update"
E.g. if author ID (aID) is changed, appropriately change authID
in foreign key
4. Derived data
Update any stored derived data when base data changes
E.g. if total number of employees is stored, add 1 if new
employee added
4 Trigger Uses
5. Security
Logging of database access
E.g. date and time each user logs on
E.g. deny access at weekend
6. Maintaining synchronous replicates
In a distributed database
7. Generating statistics on table access
5 Types of Trigger and Naming
Statement or Row triggers
So 4 types can be triggered:
Before Statement, Before Row
After Statement, After Row
Naming them
Must be unique within a schema
Can have same name as table on which it is defined but
this may be confusing to developers
Inserting,
Statement Triggers Deleting and
Updating can
6 be used to find
out which event
CREATE OR REPLACE TRIGGER trig_testTable
is occurring
AFTER INSERT or UPDATE ON Personnel
BEGIN
If Inserting
Then INSERT into testTable values ('insert done',
SYSDATE) ;
Else
INSERT into testTable values ('update done', SYSDATE)
;
End If;
END;
Author Publisher
aID last_name forename name country
1 Adriaans Pieter Addison_Wesley UK
3 Carter John Course Technology
4 Connolly Thomas E Edwin Mellen Press
5 Connolly Thomas M Longman Higher
6 Garcia-Molina Hector McGraw-Hill UK
7 Hernandez Michael J Prentice-Hall USA
8 Rob Peter
Referential Integrity Trigger Example
10
So remember ………….
While trigger is running, it knows the old and the new values
of the record you're updating
You need to specify which you want to refer to
Prefix column name with :new. or :old.
…
AFTER UPDATE OF name ON A record of
publisher what has
… changed
INSERT INTO publish_audit
VALUES(SYSDATE, :new.name,
:old.name);
Example trigger: business rules
12
CREATE OR REPLACE TRIGGER publish_trg
BEFORE INSERT OR UPDATE ON book
Trigger
FOR EACH ROW header
DECLARE
how_many NUMBER;
BEGIN
SELECT COUNT(*) INTO how_many FROM book
PL/SQL Block
WHERE publisher = :new.publisher;
IF how_many >= 3 then
Raise_application_error(-
20000,'Publisher' ||
:new.publisher || 'already has 3
books');
END IF;
END;
Example
13 CREATE OR REPLACE TRIGGER publish_trg
BEFORE INSERT OR UPDATE ON book
FOR EACH ROW
label
Message text
16 The Trigger in use
This should happen
Example:
Update to table x fires trigger
trigger queries/ modifies table x table
This is a problem!
happens only for update, not for insert
trigger
Can be fixed by using two triggers, or dummy table, but
quite tricky
(see Casteel, J. (2003). Oracle 9i Developer: PL/SQL
Programming. p.318)
18 WHEN clause
begin
insert into PriceChange values(:old.isbn,:new.price);
end;
20 Compilation errors
SHOW ERRORS
BEFORE or AFTER?
INSERT, DELETE, UPDATE?
FOR EACH ROW or once only?
Any error conditions/messages?
In Safari e-books:
Rosenzweig and Silvestrova (2003).
Oracle® PL/SQL™ by Example, 3rd ed.
Test_2
26 Getting the code
Test_3