gpt4 book ai didi

oracle - 使用 Oracle SET CONSTRAINTS ALL DEFERRED 查找所有外键错误

转载 作者:行者123 更新时间:2023-12-04 06:42:00 30 4
gpt4 key购买 nike

我在用:

set constraints all deferred;
(lots of deletes and inserts)
commit;

这按预期工作。如果存在任何断开的关系,则提交失败并引发错误,列出其失败的 FK 之一。

用户修复有问题的数据并再次运行。然后遇到另一个 FK 问题并重复该过程。

我真正想要的是一次性列出所有会导致提交失败的 FK。

我当然可以通过 select 语句(每个 FK 一个选择)编写一些东西来检查每个 FK 关系,但是使用延迟 session 的美妙之处在于这一切都为我处理。

最佳答案

设定的即时答案对我来说效果很好。

以另一篇文章中的 A、B、C 为例:

SQL> create table a (id number primary key);

Table created.

SQL> create table b (id number primary key, a_id number, constraint fk_b_to_a foreign key (a_id) references a deferrable initially immediate);

Table created.

SQL> create table c (id number primary key, b_id number, constraint fk_c_to_b foreign key (b_id) references b deferrable initially immediate);

Table created.

SQL> insert into a values (1);

1 row created.

SQL> insert into b values (1,1);

1 row created.

SQL> insert into c values (1,1);

1 row created.

SQL> commit;

Commit complete.

我有一组一致的数据......我的起点。
现在我开始一个 session 来更新一些数据——这就是我试图在我的帖子中描述的。
SQL> set constraints all deferred;

Constraint set.

SQL> delete from a;

1 row deleted.

SQL> delete from b;

1 row deleted.

SQL> insert into b values (10,10);

1 row created.

SQL> set constraint fk_b_to_a immediate;
set constraint fk_b_to_a immediate
*
ERROR at line 1:
ORA-02291: integrity constraint (GW.FK_B_TO_A) violated - parent key not foun


SQL> set constraint fk_c_to_b immediate;
set constraint fk_c_to_b immediate
*
ERROR at line 1:
ORA-02291: integrity constraint (GW.FK_C_TO_B) violated - parent key not foun

这告诉我两个破坏的约束(C 到 B)和(B 到 A),而不回滚事务。这正是我想要的。

谢谢

关于oracle - 使用 Oracle SET CONSTRAINTS ALL DEFERRED 查找所有外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/278871/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com