You are on page 1of 18

Triggers

CMPSC245
Triggers
• Trigger is a PL/SQL block associated with a specific
action taken on a table or view
• Automatically executed when a particular event
occurs
– Insert
– Update
– Delete
• For example, whenever an employee’s salary is
changed, the change must be recorded in a logging
table
Triggers

• timing: When the trigger fires in relation to the triggering event


– Values are BEFORE, AFTER, or INSTEAD OF
• event: which DML operation causes the trigger to fire
– Values are INSERT, UPDATE[OF column], and DELETE
• object_name: the table or view associated with the trigger
• trigger_body: the actions performed by the trigger and defined in
an anonymous block
Triggers Timing
• When should the trigger fire?
– Before (most common):
• Execute the trigger body before the triggering
– After
• Execute trigger body after the triggering DML event on a table
– INSTEAD OF
• Execute the trigger body instead of the triggering DML event on
view
Trigger Timings and Event Examples
• The first trigger executes immediately before an employee’s
salary is updated:

• The second trigger executes immediately after an employee is


deleted:
Trigger Timings and Event Examples
• You can restrict an UPDATE trigger to updates of a specific
column or columns

• A trigger can have more than one triggering events:


Row Trigger
• A row trigger fires once for each row affected by the
triggering DML Statement, either BEFORE or AFTER
• If five employees are in department 50, a row trigger
associated with an UPDATE on the employee table
would execute five times, storing five rows in the log
file because of the following DML statement:
Creating a Row Trigger

• You specify a row trigger using FOR EACH ROW.


• With this trigger, the UPDATE statement from the previous
slide would cause five rows to be inserted into the log file,
one for each EMPLOYEE row updated
Using :OLD and :NEW Qualifiers
• When using a row trigger, you can reference and use
both old and new column values in the EMPLOYEEs
row currently being updated
• You use :OLD.column_name to reference the pre-
update value, and :NEW.column_name to reference
the post-update value
Using :OLD and :NEW Qualifiers
• For example, if the UPDATE statement is changing an
employee’s salary from $10,000 to $11,000, then
while the trigger is executing:
– :OLD.salary has a value of 10000
– :NEW.salary has a value of 11000
Bind Variables :old and :new Defined
DML Statement :OLD :NEW
INSERT Undefined – all column Stores the values that will
values are NULL as there is be inserted into the new
no “old” version of the data row for the table.
row being inserted.
UPDATE Stores the original values Stores the new values for
for the row being updated the row – values the row
before the update takes will contain after the
place. update takes place.
DELETE Stores the original values Undefined – all column
for the row being deleted values are NULL as there
before the deletion takes will not be a “new” version
place. of the row being deleted.
INSTEAD OF Trigger
• A complex View(for example a view based on a join) cannot
be updated
• Suppose the EMP_DERAILS view is complex view based on a
join of EMPLOYEES and DEPARTMENTS
• The following SQL statement fails:
INSERT INTO emp_details
VALUES(9001, ‘ABBOTT’, 3000, 10, ‘Administration’);
• You can overcome this by changing a trigger that updates the
two base tables directly instead of trying(and failing) to
update the view
• INSTEAD OF triggers are always row triggers
INSTEAD OF Trigger
Creating an INSTEAD OF Trigger
• Step 1: Create the tables and the complex View
Creating an INSTEAD OF Trigger
• Step 2: Create the INSTEAD OF Trigger
Trigger Event Predicates
• The trigger event predicates, UPDATING, DELETING,
and INSERTING can only be used in a trigger to identify
the event that activated the trigger.
Trigger Event Predicate Example
CREATE OR REPLACE TRIGGER trgger_test
BEFORE INSERT OR UPDATE OR DELETE ON employees
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Updating salary');
ELSIF UPDATING THEN
DBMS_OUTPUT.PUT_LINE(‘UPDATING’);
ELSIF DELETING THEN
DBMS_OUTPUT.PUT_LINE(‘DELETING’);
END IF;
END;
Changing the status of Trigger
• Get Trigger Information
SELECT * FROM USER_TRIGGERS;
• Disable or re-enable a trigger
ALTER TRIGGER trigger_name DISABLE|ENABLE;
• Disable or re-enable all triggers for a table
ALTER TABLE table_name DISABLE|ENABLE ALL TRIGGERS;
• Remove a trigger
DROP TRIGGER trigger_name;

You might also like