我看到的唯一选择是将 FK 约束更改为“DEFERRABLE”。
然后,您可以在一个事务中更改两行,因为在提交数据时会检查约束:
update ROUTE_DETAILS set ROUTE_NUMBER ='RN0003' where ROUTE_NUMBER ='RN';
update CUSTOMER_DETAILS set ROUTE_NUMBER ='RN0003' where ROUTE_NUMBER ='RN';
commit;
有关如何更改 FK 约束的详细信息,请参阅手册。
如果将约束设置为“INITIALLY IMMEDIATE”,则需要set constraints deferred
在运行更新之前运行。
编辑,这是一个完整的例子:
设置表和约束:
create table route_details
(
route_number varchar(20) not null primary key
);
create table customer_details
(
cust_code varchar(20) not null primary key,
route_number varchar(20) not null
);
alter table customer_details
add constraint fk_route_number
foreign key (route_number)
references route_details (route_number)
deferrable
initially immediate;
insert into route_details (route_number)
values ('RN0001');
insert into route_details (route_number)
values ('RN');
insert into customer_details (cust_code, route_number)
values ('CC0001', 'RN0001');
insert into customer_details (cust_code, route_number)
values ('CC0002', 'RN');
commit;
运行更新:
set constraints all deferred;
update ROUTE_DETAILS set ROUTE_NUMBER ='RN0003' where ROUTE_NUMBER ='RN';
update CUSTOMER_DETAILS set ROUTE_NUMBER ='RN0003' where ROUTE_NUMBER ='RN';
commit;