ITPub博客

首页 > 数据库 > Oracle > 触发器

触发器

原创 Oracle 作者:xlqman 时间:2010-07-30 18:24:06 0 删除 编辑

先建立一个表:

create table emp2_log values(uname varchar2(20),action varchar2(10),atime date);

如果往这个表中插入,删除,更改就会触发

create or replace trigger tri

after insert or delete or update on emp2 for each row

begin

if inserting then

insert into emp2_log values(user,'insert',sysdate);

elsif updating then

insert into emp2_log values(user,'update',sysdate);

elsif deleting then

insert into emp2_log values(user,'delete',sysdate);

end if;

end;

/

对于上面的for each row 语句在执行这条语句update emp2 set sal=sal*2 where deptno = 20;更新了5行,有for each row就在emp2_log里记录5条,没有for each row就只有一条记录。

如何成功执行update emp2 set deptno =88 where deptno2;

通常情况这样是不能执行成功的,因为有PK。但可以通过触发器完成这样的操作:

create or replace trigger trig

after update on dept for each row

begin

update emp2 set deptno = :NEW.deptno where deptno = :OLD.deptno;

end;

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21548840/viewspace-1035708/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 游标的部分说明
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-08-22

  • 博文量
    22
  • 访问量
    273277