Professional Documents
Culture Documents
■ objectives :
–explain what is database trigger
–list the advantages of triggers
–list types of triggers
–state & explain the syntax for creating a trigger
–construct database triggers
■ firing of a trigger :
–the act of executing a trigger is known as firing the
trigger
–the triggering event is a dml (insert, update or delete)
operation on a database table
■you can define a trigger on a table, which are based
on dml statements (insert, delete and update)
■you can define trigger on ddl events (create, drop
and alter)
■you can also define trigger on database events
use of triggers
■enforcing complex business rules
–triggers
can be used to implement complex integrity constraints
not possible through declarative constraints in table creation
■audit operations
–auditinginformation in a table by recording the changes made and
who made them
–log events transparently based on dml operations
■ maintain replicate tables/signaling other tables
–triggers
can be used for automatically signaling other
programs that action need to take place when changes
are made to the table
■ computing values
–atrigger can be used to derive column values
automatically
■ security
–triggers
can be used to implement complex security
authorisations
types of triggers
■ triggers are categorised on
–level
–time of firing
–triggering event
types of triggers
■ trigger types on the basis of level :
–statement trigger
–row trigger
statement trigger
■the trigger action is performed once for the operation that
fires the trigger
■used when the trigger action is independent of
–the data provided by the operation that fires the trigger
–the number of affected rows
■example : maintaining the log of operation on a particular
table
row trigger
■the trigger action is performed repeatedly once for each
row that is affected by the operation
■used when the trigger action is dependent on
–the data provided by the operation that fires the trigger
–the number of affected rows
triggering time
■the triggers can specify the time of trigger action
■possible option for the time of a trigger action are
–before the triggering event
–after the triggering event
before trigger
■the trigger action is performed before the operation, that
fires the trigger, is executed
■used when the execution of operation depends on trigger
action
■example : in a bank account, before debiting an account,
checking whether the current balance is greater than the
amount to be debited
after trigger
■the trigger action is performed after the operation, that fires
the trigger, is executed
■this trigger is used when triggering action depends on the
execution of operation
triggering events
■triggering events are the dml operations
■triggering events divide triggers into following types
–delete triggers
–insert triggers
–update triggers
trigger fundamentals
■ trigger components :
–a database trigger has got following components
■a triggering event
■a trigger constraint
■a trigger action
creating a trigger
■create trigger command is used
create [or replace] trigger <trigger_name>
[ before | after ]
[ delete | [or] insert | [or] update
[ of < column1>, [<column2> …. ]
on <table_name>
[ for each row [when <condition>]]
begin
….
-- pl/sql code
….
end;
■trigger_name :
–trigger_name is the name of the trigger
■triggering event :
–triggering event specifies operation that fires the trigger
■table_name :
–table_name is the table for which the trigger is defined
■trigger_body :
–trigger_body is the main code for the trigger
■trigger_condition :
–the trigger_condition in the when clause, if present, is evaluated
first
–the body of the trigger is executed only when this condition
evaluates to true
restrictions on triggers
–a trigger can not issue any transaction control statements
commit, rollback or savepoint
–there
are restrictions on which tables a trigger body may
access, depending on the type of trigger and the
constraints on the tables
managing triggers
■replacing triggers
■dropping triggers
replacing triggers
■you can not explicitly alter a trigger
■you must recreate it with a new trigger definition
dropping triggers
■you drop a trigger when you do not want to use it
any more
■syntax :
–drop trigger trigger_name;
using :new
■ example:
create trigger generateid
before insert on emp
for each row
begin
select emp_sequence.nextval
into :new.empno from dual;
end generateid;
statements
■there has to be a mechanism for the trigger body to
example 1
■create a trigger for emp table to check the salary range,
during data manipulation. salary range is specified in
salmast table and the salary range check is not applicable
for ‘president’.
■salmast table columns
–job
–minsal
–maxsal
solution:
create or replace trigger check_salary
before insert or update
of sal, job
on emp
for each row
when (new.job !=‘president’)
declare
v_minsal number;
v_maxsal number;
begin
select minsal, maxsal into v_minsal, v_maxsal
from salmast
where job = :new.job;
if ( :new.sal < v_minsal or :new.sal > v_maxsal ) then
raise_application_error ( -20200, ‘salary out of range’);
end if;
end check_salary;
example 2
■ empstats table stores the summary based on
employee information. create a trigger that
maintains empstats table.
■ empstats table :
–deptno
–totalemp
–totalsal
example 3
■create a trigger that audits an emp table. the
operations table is used for auditing.
■audit table :
–operation
–ontable
–date
–user
additional examples
1)
create or replace trigger test_trigger1
before insert on test_trigger
begin
if( trim(' ' from to_char(sysdate,'day'))= 'saturday') then
raise_application_error(-20001,'no operations on week-end');
end if;
end;
2)
create or replace trigger test_trigger2
before update on test_trigger
for each row
begin
if (:new.age >100) then
raise_application_error(-20002,'--age not within limits');
end if;
end;
3)
create or replace trigger mutating_trigger
after update on student
for each row
v_age number(3);
begin
select age into v_age from student where name ='amit';
end;
4)
create or replace trigger dd_trigger after alter on database
begin
dbms_output.put_line('ddl trigger');
end;
ddl events
1) create
2) alter
3) drop
5)
create or replace trigger system_triger
after logon on database
begin
insert into test_trigger values (400,null,null);end;
database events
1) after servererror
2) after logon
3) before logoff
4) after startup
5) before shutdown