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';

猜你喜欢

转载自www.cnblogs.com/mobai95/p/12421631.html