1. 程式人生 > >觸發器after和before

觸發器after和before

p s 而後 color back lac per scott each 觸發器

--after和before
CREATE OR REPLACE TRIGGER del_emp1
               BEFORE update OR insert OR delete ON scott.emp
         REFERENCING new AS nn  old AS oo
         FOR EACH ROW
         WHEN (nn.sal > 2000)
BEGIN
         IF INSERTING THEN
            dbms_output.put_line(‘THE OPERATION IS INSERT‘);
         ELSIF UPDATING THEN
             dbms_output.put_line(
‘THE OPERATION IS UPDATE‘); ELSIF DELETING THEN dbms_output.put_line(‘THE OPERATION IS DELETE‘); ELSE dbms_output.put_line(‘OTHERS OPERATION‘); END IF; END; CREATE OR REPLACE TRIGGER del_emp2 after update OR insert OR delete ON scott.emp REFERENCING
new AS nn old AS oo FOR EACH ROW WHEN (nn.sal > 2000) BEGIN IF INSERTING THEN dbms_output.put_line(‘THE OPERATION IS INSERT‘); ELSIF UPDATING THEN dbms_output.put_line(‘THE OPERATION IS UPDATE‘); ELSIF DELETING THEN dbms_output.put_line(
‘THE OPERATION IS DELETE‘); ELSE dbms_output.put_line(‘OTHERS OPERATION‘); END IF; END; update emp set sal=7000 where emp.ename=‘SMITH‘; --after和before的區別是:before是先執行begin後觸發觸發器,after是先執行觸發器而後執行begin內容. CREATE OR REPLACE TRIGGER check_emp BEFORE update OR insert OR delete ON scott.emp REFERENCING new AS nn old AS oo FOR EACH ROW /*WHEN (nn.sal < 2000)*/ BEGIN IF INSERTING THEN dbms_output.put_line(‘THE OPERATION IS INSERT‘); ELSIF UPDATING THEN dbms_output.put_line(‘THE OPERATION IS UPDATE‘); ELSIF DELETING THEN dbms_output.put_line(‘THE OPERATION IS DELETE‘); ELSE dbms_output.put_line(‘OTHERS OPERATION‘); END IF; END; select * from emp; begin savepoint s1; delete emp where sal=4000; rollback to s1; end;

觸發器after和before