代码改变世界

ORA-02429: cannot drop index used for enforcement of unique /primary key

2014-06-19 17:05  潇湘隐者  阅读(13094)  评论(0编辑  收藏  举报

相信不少人遇到过ORA-02429: cannot drop index used for enforcement of unique /primary key 这个错误,对应的中文提示“ORA-02429: 无法删除用于强制唯一/主键的索引”,其实从错误提示信息已经很明显了。下面还是用一个简单的例子述说一下该错误的来龙去脉。

ORA-02429错误的原因是因为用户试图删除一个用于强制唯一/主键的索引,解决方法也很简单,删除对应的约束就会自动删除该索引。

[oracle@DB-Server ~]$ oerr ora 2429

02429, 00000, "cannot drop index used for enforcement of unique/primary key"

// *Cause: user attempted to drop an index that is being used as the

// enforcement mechanism for unique or primary key.

// *Action: drop the constraint instead of the index.

1:新建测试表TAB_TEST, 如下所示:

CREATE TABLE TAB_TEST
(
  JOB_ORDER_NO    VARCHAR2(20 BYTE),
  DIMM_ID         NUMBER,
  MRP_GROUP_CD    VARCHAR2(10 BYTE),
  ITEM_CAT        VARCHAR2(20 BYTE),
  REQUIRED_DATE   DATE,
  PURCHASED_BY    VARCHAR2(10 BYTE),
  USED_BY         VARCHAR2(10 BYTE),
  SUPPLIER_CD     VARCHAR2(10 BYTE)
)

2:添加主键约束,如下所示

ALTER TABLE TAB_TEST
 ADD CONSTRAINT PK_TAB_TEST
  PRIMARY KEY
  (JOB_ORDER_NO, DIMM_ID, MRP_GROUP_CD, ITEM_CAT);

3:查看测试表的索引信息

SQL> SELECT INDEX_NAME FROM DBA_INDEXES WHERE TABLE_NAME='TAB_TEST';
 
INDEX_NAME
------------------------------
PK_TAB_TEST

4:查看测试表的约束信息:

SQL> SELECT CONSTRAINT_NAME FROM DBA_CONSTRAINTS WHERE TABLE_NAME='TAB_TEST';
 
CONSTRAINT_NAME
------------------------------
PK_TAB_TEST

5:删除测试表的索引PK_TAB_TEST

SQL> DROP INDEX PK_TAB_TEST;
DROP INDEX PK_TAB_TEST
           *
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key

clip_image001

解决方法:

删除对应的约束就会自动删除该索引。而不是直接去删除该索引。很多菜鸟就会直接这样做!

SQL> ALTER TABLE TAB_TEST DROP CONSTRAINT PK_TAB_TEST;
 
Table altered.
 
SQL> SELECT CONSTRAINT_NAME FROM DBA_CONSTRAINTS WHERE TABLE_NAME='CONSTRAINT_NAME';
 
no rows selected
 
SQL> SELECT INDEX_NAME FROM DBA_INDEXES WHERE TABLE_NAME='TAB_TEST';
 
no rows selected
 
SQL> 
 

clip_image002