相信不少人遇到過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
解決方法:
刪除對應的約束就會自動刪除該索引。而不是直接去刪除該索引。很多菜鳥就會直接這樣做!
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>