Types of Trigger1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Briefly explain various types of Triggers in PL/SQL?

Types of Triggers: Type of trigger firing, level at which a trigger is executed, and the types of events form
the basis classification of triggers into different categories. The broad classification of triggers is as shown
below.
I. On the Basis of Type of Events
1. Triggers on System events
2. Trigger on User events
II. On the Basis of the Level at which Triggers are Executed
3. Row Level Triggers
4. Statement Level Triggers
III. On the Basis of Type of Trigger/Firing (or) Triggering Transaction
5. BEFORE Triggers
6. AFTER Triggers.
7. INSTEAD OF
1. Triggers on System Events.
System events that can fire triggers are related to instance startup and shutdown and error messages.
 STARTUP triggers fire when the database is opened by an instance,
 SHUTDOWN triggers fire just before the server starts shutting down an instance.
 SERVERERROR triggers fire when a specified error occurs, or when any error occurs if no error
number is specified.

The DBMS_AQ package is one example of using database triggers to perform certain actions. For example,
a database shutdown trigger is defined at the database level
CREATE TRIGGER register_shutdown ON DATABASE
SHUTDOWN
BEGIN
.........
DBMS_AQ.ENQUEUE(...);
.........
END;
2. Triggers on user events: User events that can fire triggers are related to
 User logon and logoff
 DDL statements (CREATE, ALTER, and DROP)
 DML statements (INSERT, DELETE, and UPDATE)

3. Row Level Triggers:


A row trigger is fired each time the table is affected by the triggering statement. For example, if an UPDATE
statement updates multiple rows of a table, a row trigger is fired once for each row affected by the UPDATE
statement. If a triggering statement affects no rows, a row trigger is not run.
For row-level trigger would look like this.

create trigger row_level_trigger


after update on Highschooler
for each row
when New.grade=13
begin
update Highschooler set grade=NULL
where New.ID = Highschooler.ID;
end;

4. Statement level Triggers:


A statement trigger is fired once on the triggering statement, and the number of rows in the table and that
the triggering statement affects, even if no rows are affected. For example, if a DELETE statement deletes
several rows from a table, a statement_level DELETE trigger is fired only once
E.g.
CREATE OR REPLACE TRIGGER before_customer
BEFORE DELETE OR UPDATE OR INSERT
ON customer
begin
IF to_char (sysdate, 'hh24') < '08" OR
to_char (sysdate, hh24')> '18'
THEN raise application_error(-20000.
Data may not be modified at this time (');
END IF;
End;

This trigger tests time of the update against the CUSTOMER table and rejects any updates before 8AM or
after 6PM.

5. Before Triggers:
BEFORE triggers execute the trigger action before the triggering statement is executed.
Trigger Type Combinations: You can create 2 types of row and statement
triggers
 BEFORE statement trigger.
Before executing the triggering statement, the trigger action is run.
 BEFORE row trigger
Before modifying each row affected by the triggering statement and before checking appropriate
integrity constriants, the trigger action is run

SQL> CREATE OR REPLACE TRIGGER passeneger_bef_del


BEFORE DELETE ON passenger_det
DECLARE
SUNDAY EXP EXCEPTION:
BEGIN
IF TO CHAR(SYSDATE, DY').. 'SUN' THEN
RAISE SUNDAY_EXP; 8 END IF:
EXCEPTION
WHEN SUNDAY_EXP THEN
RAISE APPLICATION ERROR (-20001, 'can not DELETE on SUNDAY'):
END;
/
Trigger created.

6. AFTER Trigers:
AFTER triggers execute the trigger action after the triggering statement is executed.
Trigger type combinations: You can crate 2 types of row and statement triggers
 AFTER statement trigger : After executing the triggering statement and applying any deferred
integrity constraints, the trigger action is run.
 AFTER row trigger : After modifying each row affected by the triggering statement and possibly
applying appropriate integrity constraints, the trigger action is run for the current row provided the
trigger restriction was not violated.

SQL> CREATE OR REPLACE TRIGGER PASSENGER DEL AFT AFTER DELETE ON RESERV DET
FOR EACH ROW
BEGIN
DELETE FROMPASSENGER DET WHERE PASSENGER DET .PASSNO.. OLD.PASSNO:
END;
/
Trigger created.
7. INSTEAD-OF Triggers:
INSTEAD-OF triggers are used to tell Oracle what to do instead of performing the actions that executed the
trigger. It is applicable to both object views and standard relational database. This trigger can be used to
redirect table inserts into a different table or to update different tables that are the part of the view.

You might also like