Oracle触发器

--触发器
-- :old 代表以前的列值
-- :new 更改以后的列值
--这两个变量只能在 for each row存在的时候才能使用
--update语句有:old :new
--insert只有:new
--delete只有:old

--创建一个teacher_log (只要有人操作这个teacher表)

create table teacher_log
(
logid number not null,
old_value varchar2(200),
create_date date,
o_type number,
tno number
)
--给logid增加主键约束
alter table teacher_log
add constraint pk_teacher_logid primary key(logid)

--创建序列
create sequence sq_teacher_logid
minvalue 1
maxvalue 999999999999
start with 1
increment by 1
cache 20;

--创建触发器
create or replace trigger tr_teacher
after insert or update or delete
on teacher for each row

--声明
declare
v_old_type teacher_log.o_type%type;
v_lod_value teacher_log.old_value%type;
v_tno teacher_log.tno%type;

begin
if inserting then
v_old_type:=1;
v_tno:=:new.tno;
v_lod_value:=:new.tname||','||:new.tno;
elsif deleting then
v_old_type:=2;
v_tno:=:old.tno;
--看看你删除的是哪一个数据
v_lod_value:=:old.tname||','||:old.sal;
else
v_old_type:=3;
v_tno:=:old.tno;
v_lod_value:=:old.tname||','||:old.sal;
end if;
insert into teacher_log values(
sq_teacher_logid.nextval,v_lod_value,sysdate,v_old_type,v_tno
);

end tr_teacher;

(以上内容来自王硕老师)

原文地址:https://www.cnblogs.com/liu-chao-feng/p/5890359.html