我写的一个触发器的小例子

我写的一个触发器的小例子,已经测试通过

表A和A_active结构相似都有如下字段

id    name   land_no

 

 

create or replace trigger t_A
before insert or update or delete on A
for each row
declare
    pbCount number;
begin
    select count(land_no) into pbCount from A_active where land_no = :new.land_no;
    if inserting or updating then
        if pbCount<=0 then
            insert into A_active (id,name,land_no) values (:new.id,:new.name,:new.land_no);
        else
            update A_active
            set id = :new.id,
            name = :new.name ,
            where land_no = :new.land_no;
        end if;
    end if;
    if deleting then
      delete from A_active where land_no = :old.land_no;
    end if;
end;

 

功能就是A表插入数据时 ,在A_active中查找,如果存在则更新,不存在则插入。

当A 表数据更新时,A_active也更新

当A表删除记录时,A_active也将相应记录删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值