ORALCE Trigger

1.利用触发器监控对特定表插入,删除,修改操作;
create or replace trigger tri_test1
  after insert or update or delete on emp
  for each row --是否需要针对每条操作的数量做关注;
begin
  if  inserting then
  insert into log values (user,'EMP',sysdate,'Insert');
  elsif updating then
  insert into log values (user,'EMP',sysdate,'Update');
  else
  insert into log values (user,'EMP',sysdate,'Delete');
  end if;
end tri_test1;
2. 利用触发器对两张表组成的视图进行更新
create or replace trigger tri_test
  instead of insert on v_test1
  for each row
declare
  -- local variables here
begin
  insert into emp(empno,ename) values (:new.empno,:new.ename);
  insert into dept(deptno,dname) values (:new.deptno,:new.dname);
end tri_test;
3. 利用触发器针对删除操作对相关表进行操作
create or replace trigger tri_test2
  after delete on dept
  for each row
declare
  -- local variables here
begin
  delete from emp where deptno = :old.deptno;
end tri_test2;
实用样例:
要求:
我有一个表app_mstr 我想当字段app_sale_conf='0'并且app_fi_conf='0'时 更新字段app_nbr(订单号)的值,app_nbr是一个8位长度的的数据,以"LS"开头,后面跟6位序列号,序列号用sequence创建,然后每产生一个加一个1 如"LS000001"
实现:
SQL>  create table app_mstr(app_sale_conf varchar2(10),app_fi_conf varchar2(10),app_nbr varchar2(8))
  2  /
表已创建。
SQL> create sequence seq_app
  2  start with 1
  3  increment by 1
  4  /
序列已创建。
SQL> edit
已写入 file afiedt.buf
  1  create or replace trigger tri_appup before insert or update on app_mstr for each row
  2  when(new.app_sale_conf='0' and new.app_fi_conf='0')
  3  begin
  4  select 'LS'||lpad(seq_app.nextval,6,'0') into :new.app_nbr from dual;
  5* end;
SQL> /
触发器已创建
SQL> insert into app_mstr values('0','0','1')
  2  /
已创建 1 行。
SQL> commit
  2  /
提交完成。
SQL> select * from app_mstr
  2  /
APP_SALE_C APP_FI_CON APP_NBR
---------- ---------- --------
0          0          LS000001

Comments

Popular posts from this blog

Nginx Proxy & Load Balance & LNMP

Snort+barnyard2+Snorby CentOS 6.5_64 Installation

ORACLE Error