Oracle trigger 学习

 第一步:  创建测试表

create table vegetable(      
  id number,       
  name varchar2(15),        
  price number(5,2),        
  p_date date
  )

create table price_change(       
  id number,        
  name varchar2(15),       
  old_price number(5,2),        
  chg_date date
  )
 
第二步:  向表中添加测试数据
  insert into vegetable 
  select 1001,'西红柿',2.02,to_date('2010-05-10 06:00:00','yyyy-mm-dd hh24:mi:ss') from dual union all 
  select 1004,'大葱',5.50,to_date('2010-06-18 12:05:56','yyyy-mm-dd hh24:mi:ss') from dual union all 
  select 1102,'胡萝卜',3.05,to_date('2010-06-01 00:10:00','yyyy-mm-dd hh24:mi:ss') from dual union all 
  select 1150,'大白菜',0.55,to_date('2010-07-08 15:20:00','yyyy-mm-dd hh24:mi:ss') from dual;
  commit;

第三步: 创建触发器
  create or replace trigger tri_price_chg 
  before update or delete or insert of price on vegetable 
  for each row when(new.price <> old.price)
   begin      

   if    inserting then ...

  elsif   deleting then ...

end;
   dbms_output.put_line('vegetable id = '|| :old.id);      
   dbms_output.put_line('old price = '|| :old.price);      
   dbms_output.put_line('new price = '|| :new.price);      
   dbms_output.put_line('the price has been changed');             
   insert into price_change(id,name,old_price,chg_date)      
   values(:old.id,:old.name,:old.price,sysdate); 
   end tri_price_chg;

第四步: 修改数据做测试
  update vegetable  set price=1.55  where id=1001;

第五步: 验证效果:
  select * from vegetable;
  --vegetable中的数据被更新
  --price_change表中通过触发器的操作记录了旧的价格数据.

 

与大家共享......


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值