Types of Trigger
1. Row-Level Trigger: A row-level trigger fires once for each row that is
affected by a triggering event.
For example, if Delete operation is defined as a triggering event for a
particular table, and a single DELETE statement deletes five rows from that
table, the trigger fires five times, once for each row.
2. Statement-Level Trigger: A statement-level trigger fires only once for
each statement. Using the previous example, if deletion is defined as a
triggering event for a particular table, and a single DELETE statement
deletes five rows from that table, the trigger fires once.
3. BEFORE Trigger: BEFORE trigger executes before the DML statement
(INSERT, UPDATE, DELETE) execute.
4. AFTER Trigger: AFTER trigger executes after the DML statement
(INSERT, UPDATE, DELETE) executed.
5. Combination Trigger: Combination trigger are combination of two trigger
type,
Before Statement Trigger, Before Row Trigger, After Statement Trigger,
After Row Trigger
Variables used in trigger
1.
NEW: New refer to the new value for column. It is represented as
:NEW.columnName, where columnName is the name of a column in the
table on which the trigger is defined.
2.
OLD: Old refer to the old value for the column. It is represented as
:OLD.columnName, where columnName is the name of a column in the
table on which the trigger is defined.
Error statement in trigger is written using
Raise_application_error(Error Number,’Error Message’);
eg: Raise_application_error(-20003,'Error:: Salary must be a positive
number');
Example:
1) Before Update: This trigger will raise before salary is updated on “Employee”
table
Create or replace trigger emp_update before update on employee for each
row