Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

TRIGGER

ORACLE PL/SQL
TRIGGERS
■ Associated with a particular table
■ Automatically executed when a
particular event occurs
• Insert
• Update
• Delete
• Others
TRIGGERS VS. PROCEDURES

■ Procedures are explicitly executed by a


user or application
■ Triggers are implicitly executed (fired)
when the triggering event occurs
■ Triggers should not be used as a lazy
way to invoke a procedure as they are
fired every time the event occurs
TRIGGERS
CREATE TRIGGER TriggerName
BEFORE [AFTER] events(s) ON TableName
[FOR EACH ROW]

DECLARE
Declaration of any local variables

BEGIN
Statements in Executable section

EXCEPTION
Statements in optional Exception section

END;
TRIGGERS
■ The trigger specification names the
trigger and indicates when it will
fire

■ The trigger body contains the


PL/SQL code to accomplish
whatever task(s) need to be
performed
TRIGGER TIMING
■ A trigger’s timing has to be
specified first
• Before (most common)
■ Trigger should be fired before the
operation
• i.e. before an insert

• After
■ Trigger should be fired after the operation
• i.e. after a delete is performed
TRIGGER EVENTS
■ Three types of events are available
• DML events
• DDL events
• Database events
DML EVENTS
■ Changes to data in a table
• Insert
• Update
• Delete
DDL EVENTS
■ Changes to the definition of
objects
• Tables
• Indexes
• Procedures
• Functions
• Others
■ Include CREATE, ALTER and DROP
statements on these objects
DATABASE EVENTS
■ Server Errors
■ Users Log On or Off
■ Database Started or Stopped
TRIGGER DML EVENTS
■ Can specify one or more events in
the specification
• i.e. INSERT OR UPDATE OR DELETE
■ Can specify one or more columns
to be associated with a type of
event
• i.e. BEFORE UPDATE OF SID OR
SNAME
TRIGGER LEVEL
■ Two levels for Triggers
- Row-level trigger
■ Requires FOR EACH ROW clause
• If operation affects multiple rows, trigger fires
once for each row affected
-Statement-level trigger :
• An event is triggered for each sql statement
executed.
• DML triggers should be row-level
• DDL and Database triggers should not
be row-level
EVENT EXAMPLES
EXAMPLE 1: CREATING TRIGGER

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.id > 0)
/*The following program creates a row-level trigger for the
customers table that would fire for INSERT or UPDATE
or DELETE operations performed on the CUSTOMERS table.
This trigger will display the salary difference between the old
values and new values */
DECLARE
sal_diff number;

BEGIN

sal_diff := :NEW.salary - :OLD.salary;


dbms_output.put_line('Old salary is: '|| :OLD.salary);
dbms_output.put_line('New salary is: '|| :NEW.salary);
dbms_output.put_line('Salary difference: '|| sal_diff);

END;
EXAMPLE 1: CREATING TRIGGER

When a record is created in the CUSTOMERS table, the display_salary_changes trigger


will be fired and it will display the following results:
EXAMPLE 2: CREATING TRIGGER

Assume we have a business rule that does not allow any insert
of new employees outside of normal working days and hours:

 Need to have a mechanism that will abort any insert


statement that is made:
- on Fridays or Saturdays,
- or before 8 am or after 6 pm.

 HOW?
Use a trigger that will get run before any insert of the
employee table
EXAMPLE 2: CREATING TRIGGER

CREATE OR REPLACE TRIGGER secure_emp


BEFORE INSERT ON employees

--Trigger will raise an exception if the insert statement is done


--outside of normal working days and hours

BEGIN

IF (TO_CHAR(SYSDATE,'DY') IN ('FRI','SAT')) OR

(TO_CHAR(SYSDATE,'HH24:MI') NOT BETWEEN '08:00' AND


'18:00') THEN

RAISE_APPLICATION_ERROR(-20500, 'You may insert' ||'


into EMPLOYEES table
only during ' ||' business hours.'); Error
returns an error to the user, and
END IF; when a database trigger fails,
the triggering statement is
END; automatically rolled back by
the Oracle server.
EXAMPLE 2: CREATING TRIGGER:
TESTING SECURE_EMP

INSERT INTO employees (employee_id, last_name, first_name,


email, hire_date, job_id, salary,
department_id)
VALUES (300, 'Smith', 'Rob', 'RSMITH', SYSDATE,
'IT_PROG', 4500, 60);
MANAGING TRIGGERS

• Disable or reenable a database trigger:

ALTER TRIGGER trigger_name DISABLE | ENABLE

• Disable or reenable all triggers for a table:

ALTER TABLE table_name DISABLE | ENABLE


ALL TRIGGERS
REMOVING TRIGGERS

To remove a trigger from the database, use the DROP


TRIGGER statement:

DROP TRIGGER trigger_name;

Example:
DROP TRIGGER secure_emp;

Note: All triggers on a table are removed when the table


is removed.

You might also like