Sei sulla pagina 1di 19

1

2
CONTENT
Database Triggers
Timing
Restriction
Row and Statement Level Triggers


3
Database Triggers
Program units that are attached to a specific
table
Execute in response to the following table
operations:
INSERT
UPDATE
DELETE
4
Uses For Database Triggers
Force related operations to always happen
Sell an item, update Quantity-On-Hand
Create a table that serves as an audit trail
Record who changes a student grade and when
they change it
5
Creating a Trigger
CREATE [OR REPLACE] TRIGGER trigger_name
[BEFORE|AFTER] [INSERT|UPDATE|DELETE] ON
table_name
[FOR EACH ROW] [WHEN (condition)]
DECLARE
declaration statements
BEGIN
executable statements
EXCEPTION
exception-handling statements
END;
6
Defining Triggers
To define a trigger, you must specify:
Statement type that causes trigger to fire
INSERT, UPDATE, DELETE
Timing
BEFORE or AFTER
Level
STATEMENT or ROW
7
Trigger Timing
BEFORE: trigger fires before statement
executes
Example: for audit trail, records grade value
before it is updated
AFTER: trigger fires after statement
executes
Example: update Quantity-On-Hand after item
is sold
8
Trigger Restrictions
You can only create triggers on tables that
you own
You must have the CREATE TRIGGER
system privilege
You cannot execute a COMMIT command
in a trigger
If the table is dropped, the tables database
triggers are dropped as well.
9
Trigger Levels
ROW: trigger fires once for each row that is
affected
Example: when adding multiple order lines, update
multiple inventory QOH values
STATEMENT: trigger fires once, regardless of
how many rows are updated
Example: for audit trail, you just want to record that
someone updated a table, but you dont care how many
rows were updated
10
Row-Level Trigger Syntax
WHEN (condition):
Optional
Specifies to fire only when a row satisfies a
certain search condition
Referencing old and new values in the
trigger body:
:OLD.field_name
:NEW.field_name
11
Example Before Trigger
Create or replace trigger student_bi
Before insert on student
For each row
Declare
V_student_id student.student_id%type;
Begin
select student_id_seq.nextval into
v_student_id from dual;
:new.student_id := v_student_id;
:new.created_by :=USER;
:new.created_date:=SYSDATE;
:new.modified_by := USER;
:new.modified_date:= SYSDATE;
End;
BEFORE INSERT
Pseudo-record, :NEW
:NEW pseudorecord is of
type
Triggering_Table%type
(i.e. student%type)
12
Example After Trigger
Create or replace trigger student_au
After update on student
For each row
When (nvl(NEW.zip, )<> OLD.zip)
trigger body
AFTER UPDATE
NOTE: In WHEN condition, the pseudorecord,
:NEW and :OLD are NOT prefixed with the :

13
Point to Note
In INSERT statement, :OLD is undefined
In DELETE statement, :NEW is undefined
When the above is used, PL/SQL does not
give syntax error but store the NULL in the
above records
14
Row-Level vs Statement Trigger
Row level:



Statement level
Create or replace trigger course_au
After update on course
For each row
Create or replace trigger enrollment_ad
After delete on enrollment
15
ExampleStatement Trigger
Create or replace trigger instructor_biud
Before insert or update or delete on instructor
Declare
V_day varchar2(10);
Begin
V_day:= rtrim(to_char(sysdate, DAY));
If v_day like (S%) then
Raise_application_error (-20000, A table
cannot be || modified during weekends);
End if;
End;
If the following statement:
Update instructor
set zip = 10025
where zip =10015;
Issues on Saturday / Sunday, the trigger
will generate the following error
message:

Update INSTRUCTOR
*
ERROR at line 1:
ORA-20000: A table cannot be
modified during weekends
ORA-06512: at
STUDENT.INSTRUCTOR_BIUD at
line 6
16
Disabling and Dropping Triggers
Syntax to drop a trigger:
DROP TRIGGER trigger_name;

Syntax to enable or disable a trigger:
ALTER TRIGGER trigger_name [ENABLE |
DISABLE];
17
Backing Up Data
create table sailors_audit(
who varchar2(30),
when_changed date,
sid number,
old_rating number,
new_rating number
);
create table sailors(
sid number,
sname VARCHAR2(30),
rating number check(rating <= 10),
age number
);
18
Backing Up Data
Q: Why AFTER Trigger?
A: Because in that case, the firing of the trigger occurs only when the inserted
data complies with the table integrity (check..)
CREATE or REPLACE TRIGGER backup_trig
AFTER UPDATE of Rating on Sailors
FOR EACH ROW WHEN (old.rating < new.rating)
BEGIN

INSERT INTO sailors_audit
VALUES (USER, SYSDATE, :old.sid,
:old.rating, :new.rating);

END;
/
19
Ensuring Upper Case
CREATE or REPLACE TRIGGER sname_trig
BEFORE INSERT or UPDATE of sname
on Sailors
FOR EACH ROW
BEGIN
:new.sname := UPPER(:new.sname);
END;
/
Why BEFORE Trigger?
Because sname is ensured to have converted to upper case before
insert or update into the table.

Potrebbero piacerti anche