Triggers
Triggers
1.
2.
3.
Trigger
The general syntax for creating a trigger
Is as follows
Create [ or replace ] trigger trigger_name
{BEFORE|AFTER} triggering_event ON table_name
[FOR EACH ROW]
[WHEN Condition]
DECLARE
Declaration statements
BEGIN
Executable statements
EXCEPTION
Exception Handling statements
END;
Triggers
Types of Triggers
The following are the different types of triggers.
Row triggers
and statement triggers
1. A Row trigger fires once for each row affected. It uses
FOR EACH ROW clause.
2. Statement Trigger fires once, irrespective of number of
rows affected in the table.
Triggers
Before and afterTriggers
While defining the trigger we can specify whether to
perform the trigger action (i.e. execute trigger body)
before or after the triggering statement. BEFORE and
AFTER triggers fired by DML statements can only be
defined on tables.
BEFORE triggers The trigger action here is run
before the trigger statement.
AFTER triggers The trigger action here is run after
the trigger statement.
Triggers
Desc user_triggers;
Select * from user_triggers;
Triggers
create or replace trigger mytrigger1
before insert or delete or update on emp
for each row
begin
if to_char(sysdate,'day') = 'SUN' then
dbms_output.put_line('today is holiday');
else
dbms_output.put_line('welcome back');
end if;
end;
.
Trigger created.
Triggers
SQL> update emp set salary='10000' where empid='a1';
welcome back
1 row updated.