oracle 触发器“new ”,”old“的使用
程序员文章站
2022-06-04 08:45:42
...
1、建表 a,b 语句如下:
create table a(id number(10) not null,name varchar2(256));
create table b(id number(10) not null,name varchar2(256),update_date date,flag int );--flag 0:新增 1:修改
2、建行级触发器如下:
create or replace trigger tr_save_change
after insert or update or delete on a
for each row
begin
case when inserting then
insert into b(id,name,update_date,flag)
values(:new.id,:new.name,sysdate,0);
when updating then
update b set name=:new.name,update_date=sysdate,flag=1 where id=:old.id;
when deleting then
delete from b where id=:old.id;
end case;
end;
3、测试如下:
insert into a(id,name) values(1,'aaa');
commit;
--查看b表
select * from b;
insert into a(id,name) values(2,'bbb');
commit;
--查看b表
select * from b;
update a set name='abab' where id=2;
commit;
--查看b表
select * from b;
delete from a where id=1;
commit;
--查看b表
select * from b;
上一篇: oracle笔记触发器
下一篇: ip route 命令介绍