Oracle用触发器解决修改主表A主键值,从表的外键值也会跟着改变的问题

create table a(
aid char(2) primary key,
name char(10)
);

create table b(
bid char(2) primary key,
name char(10),
aid char(2)
);

alter table b
add constraint a_b foreign key(aid) references a(aid)
deferrable;

create or replace trigger ud_aid
after update on a
for each row
begin
if :old.aid <>:new.aid then
update b set aid= :new.aid where aid= :old.aid;
end if;
end;

insert into a values('1','a');
insert into a values('2','b');
insert into b values('1','a','1');
insert into b values('2','b','2');

update a set aid='3' where aid='1';

posted @ 2020-03-05 17:56  墨白95  阅读(537)  评论(0编辑  收藏  举报