从多个子项 table(并非所有子项 table)中删除特定记录,然后从父项 table 条目中删除

Delete specific record from multiple child table(Not all child table) and then the parent table entry

我有一个父 table 名字 A 并且有 47 个子 table 引用了这个父 table A,我想删除 23 个子 [=22] 的记录=]s,然后是父 table,并希望将记录保留在剩余的子 tables 中。

我尝试通过禁用和启用约束来执行此操作,但在启用约束时遇到问题。

谁能提出更好的方法。

注意:我正在尝试通过 shell 脚本实现此目的。

这里的关键字是 ENABLE NOVALIDATE。它会让您保留“无效”的现有数据,但将对新添加的行强制执行参照完整性。

这是一个例子。

大师和两个细节tables:

SQL> create table master
  2    (id_mas number primary key,
  3     name varchar2(20));

Table created.

SQL> create table det_1
  2    (id_det number primary key,
  3     id_mas number constraint fk_d1_mas references master (id_mas),
  4     name varchar2(20));

Table created.

SQL> create table det_2
  2    (id_det number primary key,
  3     id_mas number constraint fk_d2_mas references master (id_mas),
  4     name varchar2(20));

Table created.

SQL>

示例行:

SQL> insert into master (id_mas, name)
  2  select 1, 'Little' from dual union all
  3  select 2, 'Foot'   from dual;

2 rows created.

SQL> insert into det_1 (id_det, id_mas, name)
  2  select 100, 1, 'Lit det 1'  from dual union all
  3  select 101, 1, 'Tle det 1'  from dual union all
  4  select 102, 2, 'Foot det 1' from dual;

3 rows created.

SQL> insert into det_2 (id_det, id_mas, name)
  2  select 200, 1, 'Lit det 2'  from dual union all
  3  select 201, 2, 'Tle det 2'  from dual union all
  4  select 202, 2, 'Foot det 2' from dual;

3 rows created.

SQL> commit;

Commit complete.

SQL>

现在,让我们从 DET_1MASTER 中删除 ID_MAS = 1,但将其保留在 DET_2:

从详细信息中删除 table 可以(为什么不可以?):

SQL> delete from det_1 where id_mas = 1;

2 rows deleted.

由于 DET_2:

的外键约束,我无法从 master table 中删除
SQL> delete from master where id_mas = 1;
delete from master where id_mas = 1
*
ERROR at line 1:
ORA-02292: integrity constraint (SCOTT.FK_D2_MAS) violated - child record found

所以,让我们禁用它:

SQL> alter table det_2 disable constraint fk_d2_mas;

Table altered.

现在从 master 删除成功:

SQL> delete from master where id_mas = 1;

1 row deleted.

Re-enabling 之前在 DET_2 上禁用的约束将 失败 因为它包含 ID_MAS 中不存在的行 MASTER table 再:

SQL> alter table det_2 enable constraint fk_d2_mas;
alter table det_2 enable constraint fk_d2_mas
                                    *
ERROR at line 1:
ORA-02298: cannot validate (SCOTT.FK_D2_MAS) - parent keys not found

如我所说,使用ENABLE NOVALIDATE:

SQL> alter table det_2 enable novalidate constraint fk_d2_mas;

Table altered.

表的内容:

SQL> select * from master;

    ID_MAS NAME
---------- --------------------
         2 Foot

SQL> select * from det_1;

    ID_DET     ID_MAS NAME
---------- ---------- --------------------
       102          2 Foot det 1

SQL> select * from det_2;

    ID_DET     ID_MAS NAME
---------- ---------- --------------------
       200          1 Lit det 2         --> this master doesn't exist any more
       201          2 Tle det 2
       202          2 Foot det 2

SQL>

让我们尝试插入一些新的(有效和无效)行:

SQL> insert into det_1 (id_det, id_mas, name)
  2  select 110, 2, 'Valid' from dual;

1 row created.

SQL> insert into det_1 (id_det, id_mas, name)
  2  select 111, 1, 'Invalid' from dual;
insert into det_1 (id_det, id_mas, name)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK_D1_MAS) violated - parent key not
found


SQL> insert into det_2 (id_det, id_mas, name)
  2  select 210, 2, 'Valid' from dual;

1 row created.

SQL> insert into det_2 (id_det, id_mas, name)
  2  select 211, 1, 'Invalid' from dual;
insert into det_2 (id_det, id_mas, name)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK_D2_MAS) violated - parent key not
found


SQL>

I am trying to achieve this via shell script.

一个shell脚本shell 与此有什么关系?这是 Oracle 的业务。