ORACLE触发器详细创建

--为cardInfo表创建insert事前触发器,实现添加账户信息时自动向userInfo表添加相应记录,需要判断改用户身份证号是否已存在,若存在则不添加,仅向cardInfo添加即可;

create or replace trigger tri_insert
  before insert on cardInfo
  for each row
begin
  declare
    card userInfo.User_Card %type;
  begin
  select user_card into card from userInfo where user_Id=:new.user_Id;  
  exception
      when no_data_found then
          insert into userInfo values(:new.user_Id,'张扬','男','15639700250','41147199101228466');
  end;
end;

commit;
insert into userInfo values(2015001,'张扬','男','15639700250','41147199101228466');

select * from userInfo;

--2、为tranInfo表创建insert触发器,实现添加交易记录时自动修改cardInfo表中相应账户的余额,注意应判断交易类型,若“支取”则余额减少,若“存入”则余额增加;

create or replace trigger tri_insert_tranifo
before insert on transInfo
for each row
  declare money cardInfo.current_money %type;
begin
    select current_money into money where card_id=:new.card_id;
  if :new.tran_type='支取' and money>=:new.tran_money  then
      update cardInfo set current_money=current_money-:new.tran_money where card_id=:new.card_id;
  else if  :new.tran_type='存入' then
          update cardInfo set current_money=current_money+:new.tran_money where card_id=:new.card_id;
    end if;
  end if;
          exception
      when no_data_found then
     
  dbms_output.put_line(:new.tran_money);
end;
--调用
declare
  trid number:=66622700||trunc(80000*dbms_random.value);
  cid char(18):=6227||to_char(sysdate,'YYYYMMDDHH24MISS');
begin  
insert into transInfo values(trid,'6221003',default,'存入',300.00);
end;
commit;
--3、为cardInfo表添加update触发器,实现修改余额时的业务规则应用,不允许余额小于0,否则报错。
create or replace trigger tri_cardInfo_update
before update on cardInfo
for each for
begin
  if :new.current_money<0then
      dbms_output.put_line('对不起,您的卡上当前余额不足!');
  end if;
end;

--  4、为transInfo表添加delete触发器,实现删除交易记录时将删除的数据进行备份,备份到tempTrans表中。
create or replace trigger tri_transinfos_delete
after delete on transinfo for each row
begin
  insert into temptrans values(:old.tran_id,:old.card_id,:old.tran_date,:old.tran_type,:old.tran_money);
end;



commit;



select * from dept;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值