Unit - 5_ADBMS
Unit - 5_ADBMS
Trigger
� Trigger is a group or set of SQL and PL/SQL statements that are executed
by oracle itself.
� Trigger is fired automatically based on database event.
� Triggers are like procedures and functions , although not exactly same.
Syntax:
Before / after insert, delete, update on table_name Old / New for each row
Declare
---------
Begin
------
Exception
------
End;
Structure of Trigger
� A trigger contains three basic sections:
1) Triggering event or statement
2) Trigger restriction
3) Trigger action
Trigger restriction
Trigger action
Types of Trigger
Following are the four types of trigger:
� Row trigger
- Fire each time a row is affected by the triggering statement.
- For example, if an update operation updates three rows in a table, trigger
will be executed three times.
- If no rows are affected by the triggering statements, the trigger will not
be executed.
For example:
create or replace trigger trig1
before update on account for each row
begin
dbms_output.put_line('Welcome for Update');
end;
� Statement trigger
- It fires only once.
- For example, if an update operation updates three rows in a table, trigger
will be executed only once.
- If no rows are affected by the triggering statements, the trigger will
execute only once.
For example:
create or replace trigger trig1
before insert on data
begin
dbms_output.put_line('Welcome');
end;
� Before trigger
- Trigger is executed before the triggering statements.
For example:
create or replace trigger trig1
before insert on data
begin
dbms_output.put_line('Welcome');
end;
� After trigger
- Trigger is executed after the triggering statements.
For example:
create or replace trigger trig1
after insert on data
begin
dbms_output.put_line('Welcome');
end;
For example,
end;
� In this example, audit and trail will manage the history of data(origional
table) and will store in info(new table) as log file or backup.
Advantages of Trigger:
Examples:
begin
end;