删除分区期间的约束错误

时间:2017-05-19 13:14:25

标签: oracle

当我试图在oracle中删除分区时,我面临下面提到的错误。

错误报告: SQL错误:ORA-02266 - “启用的外键引用的表中的唯一/主键” *原因:尝试截断具有唯一或。的表            在另一个表中启用外键引用的主键。            不允许的其他操作是删除/截断a的分区            分区表或ALTER TABLE EXCHANGE PARTITION。 *动作:在执行上述操作表之前,禁用            其他表中的外键约束。你可以看到什么            约束通过发出以下内容引用表            命令:            SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME =“tabnam”;

我几乎没有系统限制,很少由我开发,所以我需要禁用系统约束来删除分区,附加的是约束的截图。enter image description here

1 个答案:

答案 0 :(得分:0)

我相信一个例子会澄清。假设我有一个master表,已分区,并且detail表引用了带有FK的master

SQL> create table master(col1 number, part number)
  2  partition by list(part) (
  3      partition p0 values (0),
  4      partition p1 values (1),
  5      partition p2 values (2)
  6  );

Table created.

SQL> alter table master add constraint master_pk primary key ( col1, part);

Table altered.

SQL> create table detail(col1 number, part number, col2 varchar2(100));

Table created.

SQL> alter table detail add constraint master_detail_fk foreign key ( col1, part) references master ( col1, part);

Table altered.

根据FK约束我在两个表中插入一些行:

SQL> insert into master (col1, part) select level, mod(level, 3) from dual connect by level < 10;

9 rows created.

SQL> insert into detail (col1, part, col2) select level, mod(level, 3), 'something' from dual connect by level < 10;

9 rows created.

现在我尝试在主服务器中删除一个分区:

SQL> alter table master drop partition p1;
alter table master drop partition p1
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

我需要在放弃之前处理FK:

SQL> alter table detail disable constraint master_detail_fk ;

Table altered.

SQL> alter table master drop partition p1;

Table altered.

当然,如果删除引用的行,则不会验证约束:

SQL> alter table detail enable validate constraint master_detail_fk ;
alter table detail enable validate constraint master_detail_fk
                                              *
ERROR at line 1:
ORA-02298: cannot validate (SIUCREDITO.MASTER_DETAIL_FK) - parent keys not
found

SQL> alter table detail enable novalidate constraint master_detail_fk ;

Table altered.

SQL>