自己第一次写触发器(oracle)

触发器1(tr_baseinfo_oplist):
当sto_base_info表执行INSERT,UPDATE,DELETE的时候(delete非物理删除),
执行触发器往STO_STORE_OP_LIST里记录一下操作日志(这里delete是修改is_delete)

CREATE OR REPLACE TRIGGER tr_baseinfo_oplist
  AFTER  INSERT OR UPDATE  //在操作执行之后触发事件
  ON sto_base_info  //在sto_base_info表上触发
  FOR EACH ROW  //以行
BEGIN //下面就是具体的执行逻辑了
case
when updating then //当执行修改操作时执行
 if :new.is_delete != :old.is_delete THEN//判断是不是删除操作 is_delete='0'为正常
    if :new.is_delete != '0' then
         INSERT INTO STO_STORE_OP_LIST(store_id ,op_code,op_datetime)
                  VALUES(:new.store_id,'DELETE',systimestamp);
        end if;
 else//不是删除就是修改了
   INSERT INTO STO_STORE_OP_LIST(store_id ,op_code,op_datetime)
                  VALUES(:new.store_id,'UPDATE',systimestamp);
  end if;
when inserting then //当新增的时候执行
   INSERT INTO STO_STORE_OP_LIST(store_id ,op_code,op_datetime)
                  VALUES(:new.store_id,'INSERT',systimestamp);

end case;
END;

触发器2(tr_income_oplist):
当STO_INCOME_SETTING表执行INSERT,UPDATE,DELETE的时候,
执行触发器往STO_INCOMING_OP_LIST里记录一下操作日志

create or replace trigger tr_income_oplist
  before  INSERT OR UPDATE OR DELETE  //在操作执行之前触发事件
  ON STO_INCOME_SETTING
  FOR EACH ROW
BEGIN
CASE
when updating then
   INSERT INTO STO_INCOMING_OP_LIST(record_id ,op_code,op_datetime)
                  VALUES(:new.id,'UPDATE',systimestamp);
when inserting then
   INSERT INTO STO_INCOMING_OP_LIST(record_id ,op_code,op_datetime)
                  VALUES(:new.id,'INSERT',systimestamp);
 when deleting then
   delete from STO_INCOMING_OP_LIST where record_id= :old.id;
   INSERT INTO STO_INCOMING_OP_LIST(record_id ,op_code,op_datetime)
                  VALUES(:old.id,'DELETE',systimestamp);
END CASE;
END;

此处特别注意:
关键字:
:NEW 和:OLD使用方法和意义,new 只出现在insert和update时,old只出现在update和delete时。
在insert时new表示新插入的行数据,update时new 表示要替换的新数据、
old表示要被更改的原来的数据行,delete时old表示要被删除的数据。
在触发器中不必使用commit。