ORACLE触发器 trigger

1、触发器说明
   触发器是一种在事件发生时隐式地自动执行的PL/SQL块,不能接受参数,不能被显式调用。
2、触发器类型
1)DML触发器
   对数据表进行DML语句操作(如insert、update、delete)时所触发的触发器,可以分为:
   语句级触发器或行级触发器:行级触发器会对数据库表中的受影响的每一行触发一次触发器代码,语句级触发器则只触发一次,与语句所影响到的行数无关。
   before触发器或after触发器:before触发器在触发事件发生之前执行触发器代码,after触发器则在触发事件发生之后执行。
CREATE OR REPLACE TRIGGER "TRG_WF_SAFE_POLICY" before insert
on WF_SAFE_POLICY for each row
declare
s integer;
begin
     if(:new.PKID is null) then 
       select SEQ_WF_SAFE_POLICY_ID.NEXTVAL INTO :new.PKID from dual;
    else 
       select SEQ_WF_SAFE_POLICY_ID.NEXTVAL INTO s from dual;
    end if;
end;
CREATE OR REPLACE TRIGGER synchroLockAccount    //synchroLockAccount 触发器名称
 AFTER  UPDATE ON aims_account   // 当aims_account表变更之后  
 FOR EACH ROW  //指定创建的是行级触发器,若没有该子句则创建的是语句级触发器
   
 declare 
 temp_a number(15,2) ;
 temp_b int;
 
BEGIN
  if :OLD.ballimitflag = '2'  then
    select count(*) into temp_b  from aims_account_amountlock l where l.accountid = :OLD.accountid and l.ballimitflag='2';
    if temp_b >0 then
     update aims_account_amountlock lc set lc.lock_amount = :new.account_balance where lc.accountid= :OLD.accountid;
    end if; 
  end if;
END;

创建触发器:将对student表的操作都记录到stu_log表中(update of 用于指定一个或多个字段,指定字段被更新时才会触发触发器)
create or replace trigger modify_stu
after insert or delete or update of stu_name
on student
for each row
  begin 
    if inserting then
      insert into stu_log values(1,'insert',sysdate,:new.stu_name);
    elsif deleting then
       insert into stu_log values(2,'delete',sysdate,:old.stu_name);
    elsif updating then
      insert into stu_log values(3,'update_old',sysdate,:old.stu_name);
      insert into stu_log values(4,'update_new',sysdate,:new.stu_name);
     end if;
end;
语句级触发器(before触发器):用来控制对表的修改
create or replace trigger modify_stu
before insert or update or delete on student
begin
   if deleting then
     raise_application_error(-20001,'该表不允许删除数据');
   elsif updating then
     raise_application_error(-20002,'该表不允许修改数据');
    elsif inserting then
     raise_application_error(-20003,'该表不允许插入数据');
    end if;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值