1、当前oracle数据库有两个表,scott用户是系统自带的用户,里面有一个dept表,作为测试使用。同时新建一个test用户,里面也新建一个表dept。当向scott用户的dept表进行插入或者更新时,将数据也同时更新到test用户的dept表中。

2、触发器创建步骤:

(1)、使用PL/SQL DEVELPTOR 登录sys或者system用户,将test用户下dept表的插入/更新/选择权限赋给scott用户:

grant insert on test.dept to scott;
grant update on test.dept to scott;
grant select on test.dept to scott;

(1)、创建触发器:

CREATE OR REPLACE TRIGGER dept_insert_update
     AFTER update or insert on  SCOTT.DEPT
     for each row

DECLARE
    //声明变量,可以保存查询出来的值,再将该值用于更新操作等。此处没有用到
    myname varchar2 ( 22 );

begin
     case
         when inserting then
              insert into test.DEPT (DEPTNO,DNAME,LOC)             
              VALUES(:new.DEPTNO,:new.DNAME,:new.LOC);
         when updating then
              update test.DEPT SET DNAME = :new.DNAME, LOC = :new.LOC WHERE DEPTNO = :new.DEPTNO;
    end case;
end;

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐