par
Bibin Kallunkal , Oracle PLSQL Developer,ERP administrator, Oracle Developer, Software Engineer, IT Administration , Intercare
create a trigger for events DML,System and Database. The trigger will be invoked once that particular event is occurred. DML triggers are insert,delete,update etc.
par
Uzair Awan , MIS Assistant , Jamal Din Wali Sugar Mills Ltd
It depends what type of trigger you need Insert, Update or Delete and on which event you want to fire the trigger like Before Insert or After Insert, Before Update or After Update and Before Delete or After Delete. These are the standard options of any trigger which you can use. And it also depends whether you want to set trigger on Database Level or Application Level. Please note one thing that Trigger on Database Level are more powerful than Application Level because when you apply trigger on Database Level than you don't need to apply the same trigger in application level and in future if you would change the application language (front-end), suppose currently you are working in VB + Oracle Database and in future you want to change PHP + Oracle Database than you wouldn't need any changes at application level.
In general you can create trigger by the following command:
create or replace trigger trigger_name
timming ...
firing_events ...
on object_name
for each row
when conditions....
your pl/sql block goes hear...
end trigger_name;
as show above the most important is:1. timming: when you want your trigger to fire? you have3 options (before, after, instead of )2. fireing_events: what events you want your trigger to fire on? insert? update? delete?3. for each row: is your trigger will fire for each row? or for a Statement (transaction)?4. pl/sql block: the code which will executed by trigger when it fired.
example:
we have emp table as in scott/tiger user.
create table EMP
(
empno NUMBER(4) not null,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
);
/
we need to ensure that if comm is null, then we will put zero0, for every inserted or updated row, as shown in the following trigger.
create or replace trigger emp_comm_trigger
before insert or update on emp
for each row
declare
begin
if :new.comm is null then
:new.comm :=0;
end if;
end emp_comm_trigger;
now try to insert value in this table like
INSERT INTO EMP (empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno)
VALUES (9999,
'EMP001',
'MANAGER',
NULL,
SYSDATE,6500,
NULL, -- NOTE THAT COMM IS INSERTED AS NULL HERE
NULL);
COMMIT;
select t.rowid from EMP t;
you will find a value0.00 instead of null in inserted row in emp.comm column.
some tools makes creating objects like create trigger is easy, you can use wizard to create your trigger.
for detailed info about creating trigger you can refer to Oracle SQL Reference.
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/create_trigger.htm
regards,