前两天遇到一个问题,在删除一个主键的之后,插入重复的数据报错ORA-00001唯一性约束冲突

test@TESTDB> insert into t1 select * from dba_objects where rownum < 100;
insert into t1 select * from dba_objects where rownum < 100
*
ERROR at line 1:
ORA-00001: unique constraint (TEST.PK_T1_OBJECT_ID) violated

查询T1表主键及索引,发现主键索引仍然存在

test@TESTDB> select * from dba_cons_columns where table_name='T1';

no rows selected

test@TESTDB> select INDEX_OWNER,INDEX_NAME,TABLE_NAME,COLUMN_NAME from dba_ind_columns where table_name='T1';

INDEX_OWNER                    INDEX_NAME                     TABLE_NAME                     COLUMN_NAME
------------------------------ ------------------------------ ------------------------------ --------------------
TEST                           PK_T1_OBJECT_ID                T1                             OBJECT_ID

手动删除该索引,插入数据成功

test@TESTDB> drop index test.PK_T1_OBJECT_ID;

Index dropped.

test@TESTDB> insert into t1 select * from dba_objects where rownum < 100; 

99 rows created.

正常情况下删掉主键的时候会同时删除主键附带的索引,这里为什么会出现不删除的情况呢?

在MOS上面有一篇文章解答了这个疑问:

Indexes Associated With Primary Key Constraints Of Imported Tables Are Not Dropped When Constraints Are Disabled (文档 ID 887208.1)

 

由于T1表是imp导入的,而在import的时候,oracle会先创建唯一性索引,然后在添加主键约束,这就导致在创建主键的时候,唯一索引已经存在,并被主键使用,而对于并非主键创建的唯一性索引,在删除主键的时候,索引是会保留的。

如果想解决该问题,可以通过如下的方法进行修复:

test@TESTDB> alter table t1 enable constraint PK_T1_OBJECT_ID;

Table altered.

test@TESTDB>
test@TESTDB> select * from dba_cons_columns where table_name='T1';

OWNER CONSTRAINT_NAME TABLE_NAME COLUMN_NAME POSITION
------------------------------ ------------------------------ ------------------------------ -------------------- ----------
TEST PK_T1_OBJECT_ID T1 OBJECT_ID 1

test@TESTDB>
test@TESTDB> select INDEX_OWNER,INDEX_NAME,TABLE_NAME,COLUMN_NAME from dba_ind_columns where table_name='T1';

INDEX_OWNER INDEX_NAME TABLE_NAME COLUMN_NAME
------------------------------ ------------------------------ ------------------------------ --------------------
TEST PK_T1_OBJECT_ID T1 OBJECT_ID

Trackback

no comment untill now

Sorry, comments closed.

返回顶部
粤ICP备14044753号