3,491
社区成员
发帖
与我相关
我的任务
分享
--本文摘自美国David Lockman著的《轻松掌握Oracle8数据库开发》,供LZ作个参考:
--表tab1建立了一个行级before update触发器,它可将tab1中的col1列的旧值插入到tab2中;
--表tab2建立了行级的before update触发器,它可更新tab3,并用col2的新值给col3赋值;
--最后对于表tab3,建立语句级after update触发器,它可在col3的值等于27时,在表tab3中插入行。
SQL> select * from tab1;
COL1
----------
7
SQL> select * from tab2;
COL2
----------
10
SQL> select * from tab3;
COL3
----------
13
SQL>
SQL> create or replace trigger tab1_update_before before update on tab1 for each row
2 declare
3 begin
4 insert into tab2(col2) values(:old.col1);
5 end;
6 /
Trigger created
SQL>
SQL> create or replace trigger tab2_insert_before before insert on tab2 for each row
2 declare
3 begin
4 update tab3 set col3=:new.col2;
5 end;
6 /
Trigger created
SQL>
SQL> create or replace trigger tab3_update_after after update on tab3
2 declare
3 begin
4 insert into tab3(col3) values(27);
5 end;
6 /
Trigger created
SQL> update tab1 set col1=8;
1 row updated
SQL> commit;
Commit complete
--如下看到:
--在tab1中,col1的值被更新为8;
--在tab2中,触发器tab1_update_before用col1:7的旧值插入一行新数据;
--在tab3中,当在tab2中插入新行时,使触发器tab2_insert_before触发,并设置col3的值,使其与在tab2插入的值7相同。
--当tab3被更新后,触发器tab3_update_before在tab3中用值27插入另一行。
SQL> select * from tab1;
COL1
----------
8
SQL> select * from tab2;
COL2
----------
10
7
SQL> select * from tab3;
COL3
----------
7
27
SQL>