我有一个父表名a,有47个子表引用这个父表a,我想从23个子表中删除记录,然后再删除父表,并想保留其余子表中的记录。
我尝试通过禁用和启用约束来实现这一点,但在启用约束时遇到了问题。
有人能提出更好的方法吗。
注意:我正试图通过shell脚本来实现这一点。
此处的关键字是ENABLENOVALIDATE。它会让你保持";无效";现有数据,但将对新添加的行强制执行引用完整性。
下面是一个例子。
主表和两个明细表:
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_1
和MASTER
中删除ID_MAS = 1
,但将其保留在DET_2
:中
从明细表中删除是可以的(为什么不可以?(:
SQL> delete from det_1 where id_mas = 1;
2 rows deleted.
由于DET_2
:的外键约束,我无法从主表中删除
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.
从主机删除现在成功:
SQL> delete from master where id_mas = 1;
1 row deleted.
重新启用先前在DET_2
上禁用的约束将失败,因为它包含ID_MAS
不再存在于MASTER
表中的行:
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
正如我所说,使用启用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>
我正试图通过shell脚本实现这一点。
一个shell脚本?shell与此有什么关系?这是甲骨文的业务。