`
wsql
  • 浏览: 11811267 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

cannot drop index

 
阅读更多
drop index时出现如下错误:
SQL> drop index oos_index;

drop index oos_index

*

ERROR at line 1:

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

我们知道当创建Primary key和unique约束时,如果在该key上不存在索引,则Oracle会自动创建对应的unique索引,而当你要删除该索引时,必须先Disable或Drop该约束。看下面的例子:

SQL>CREATE TABLE employees
2 (
3 empno NUMBER(6) PRIMARY KEY,
4 name VARCHAR2(30),
5 dept_no NUMBER(2)
6 );

Table created.

SQL> select index_name,owner,table_NAME from all_indexes where owner=’SFA’ AND table_name=’EMPLOYEES’;

INDEX_NAME OWNER TABLE_NAME
———————- ——————- —————–
SYS_C007594 SFA EMPLOYEES

SQL> SELECT CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,INDEX_NAME FROM ALL_CONSTRAINTS WHERE TABLE_NAME=’EMPLOYEES’;

CONSTRAINT_NAME C TABLE_NAME INDEX_NAME
—————————— — ————————- ——————
SYS_C007594 P EMPLOYEES SYS_C007594

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

SQL> ALTER TABLE employees MODIFY PRIMARY KEY DISABLE; (大笑)

Table altered.

SQL> select index_name,owner,table_NAME from all_indexes where owner=’SFA’ AND table_name=’EMPLOYEES’;

no rows selected

SQL> SELECT CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,INDEX_NAME FROM ALL_CONSTRAINTS WHERE TABLE_NAME=’EMPLOYEES’;

CONSTRAINT_NAME C TABLE_NAME INDEX_NAME
—————————— — ————————- ——————
SYS_C007594 P EMPLOYEES SYS_C007594

SQL> ALTER TABLE employees
2 MODIFY PRIMARY KEY ENABLE;

Table altered.

SQL> select index_name,owner,table_NAME from all_indexes where owner=’SFA’ AND table_name=’EMPLOYEES’;

INDEX_NAME OWNER TABLE_NAME
——————- —————– ————-
SYS_C007594 SFA EMPLOYEES

从上面可以看出,如果创建了Primary Key约束,则Oracle会自动帮你创建相应的unique索引。当把Primary Key约束Disable时会自动删除对应的Unique索引,而重新将该约束Enable时,Oracle会重建
Unique索引。特别要注意:当Disable PK或Unique约束时,Oracle只会删除对应的Unique索引。可参考Oracle文档中的解释:

If you enable a unique or primary key constraint, and if no index exists on the key, Oracle creates a unique index. This index is dropped if the constraint is subsequently disabled, and Oracle rebuilds the index every time the constraint is enabled.

To avoid rebuilding the index and eliminate redundant indexes, create new primary key and unique constraints initially disabled. Then create (or use existing) nonunique indexes to enforce the constraint. Oracle does not drop a nonunique index when the constraint is disabled, so subsequent ENABLE operations are facilitated.


在Oracle 9i中,用于支持Primary Key和Unique Key约束的索引可独立于约束本身,实现方法是在CREATE TABLE或ALTER TABLE时指定USING INDEX子句,例子如下:

SQL> CREATE TABLE employees
2 (empno NUMBER(6),
3 name VARCHAR2(30),
4 dept_no NUMBER(2),
5 CONSTRAINT emp_pk PRIMARY KEY(empno)
6 USING INDEX
7 (CREATE INDEX emp_pk_idx ON employees(empno) TABLESPACE indx)
8 );

Table created.

SQL> select index_name,owner,table_NAME from all_indexes where owner=’SFA’ AND table_name=’EMPLOYEES’;

INDEX_NAME OWNER TABLE_NAME
————————— ————- ———————-
EMP_PK_IDX SFA EMPLOYEES

SQL> SELECT CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,INDEX_NAME FROM ALL_CONSTRAINTS WHERE TABLE_NAME=’EMPLOYEES’;

CONSTRAINT_NAME C TABLE_NAME INDEX_NAME
—————————— — ——————– —————
EMP_PK P EMPLOYEES EMP_PK_IDX

这样做的好处是:

1。可将索引存储在指定的表空间中,从而与表分离

2。通过创建一个非唯一索引,让PK或Unique Key使用,可避免在Enable或Disable PK或Unique Key时重建索引,同时可以消除多余的索引。

在删除约束时可选择保留索引:
1。ALTER TABLE employees DROP PRIMARY KEY KEEP INDEX;
2。ALTER TABLE employees DROP CONSTRAINT emp_pk;–对应的索引必须为非唯一索引

对于第二条语句,必须是对应的索引为非唯一索引,否则会连索引一并删除。

分享到:
评论

相关推荐

    pandas索引(loc,[],iloc,query,at/iat,cut,set_index,sort_index,reset_index,where,drop_duplicates…)

    索引层的交换(a)swaplevel方法(两层交换)(b)reorder_levels方法(多层交换)三、索引设定1.index_col参数2.reindex和reindex_like3.set_index和reset_index4.rename_axis和rename四、常用索引型函数1.where

    微软内部资料-SQL性能优化5

    Each index row in node pages contains an index key (or set of keys for a composite index) and a pointer to a page at the next level for which the first key value is the same as the key value in the ...

    Delphi7.1 Update

    If you encounter an empty Help Index tab after installing this update, open the d7.ohi file in the Delphi7Help folder and comment out one or more of the index entries by changing the colon in column ...

    WinMenc 0.81

    Fixed: speed/quality control index problem Fixed: job status progress cannot complete Fixed: priority dropdownlist problem Added: two new profiles, Film-to-NTSC and Film-to-PAL Added: preset ...

    WinMenc 0.81 Beta

    Fixed: speed/quality control index problem Fixed: job status progress cannot complete Fixed: priority dropdownlist problem Added: two new profiles, Film-to-NTSC and Film-to-PAL Added: preset values ...

    SQL21日自学通

    DROP INDEX498 DROP PROCEDURE498 DROP TABLE498 DROP TRIGGER 499 DROP VIEW499 EXECUTE499 FETCH499 FROM499 GRANT500 GROUP BY500 HAVING500 INTERSECT500 ORDER BY500 ROLLBACK TRANSACTION 500 REVOKE500 ...

    DevExpress VCL 12.2.6(v2012vol2.6) 源码-例子-帮助-part1

    B234839 - TcxBarEditItem - ColorEdit - White color cannot be selected within the editor drop-down window ExpressDocking Library B233009 - An AV occurs when debugging a docking operation with ...

    DevExpress VCL 12.2.6(v2012vol2.6) 源码-例子-帮助-part2

    B234839 - TcxBarEditItem - ColorEdit - White color cannot be selected within the editor drop-down window ExpressDocking Library B233009 - An AV occurs when debugging a docking operation with ...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part2

    B238931 - Export - Files exported to XLSX cannot be opened with OpenOffice or LibreOffice Q520691 - The dxHalfOfPi constant declaration in cxGeometry.hpp causes warnings when generating precompiled ...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part1

    B238931 - Export - Files exported to XLSX cannot be opened with OpenOffice or LibreOffice Q520691 - The dxHalfOfPi constant declaration in cxGeometry.hpp causes warnings when generating precompiled ...

    微软内部资料-SQL性能优化3

    Index An entire index of a table. Table An entire table, including all data and indexes. Extent A contiguous group of data pages or index pages. Page An 8-KB data page or index page. Key Row lock ...

    hls.min.js

    FRAG_BUFFERED:"hlsFragBuffered",FRAG_CHANGED:"hlsFragChanged",FPS_DROP:"hlsFpsDrop",FPS_DROP_LEVEL_CAPPING:"hlsFpsDropLevelCapping",ERROR:"hlsError",DESTROYING:"hlsDestroying",KEY_LOADING:...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    index (counted from the first index instead of the first list item). - FIX: In THistory.DeleteActions added validation of FActionIndex (FActionIndex could appear beyond the list boundaries). Thanks ...

    VB编程资源大全(英文源码 控制)

    listtxt.zip This shows how to retrieve the text from the current selection in a listbox.<END><br>11 , list_index.zip This example tells you if the inputted List Index is selected or not.<END>...

    VB编程资源大全(英文源码 网络)

    I don't know the author's name so cannot give them credit. But basically this sample will return the Ethernet Address of the card in the current machine.<END><br>61 , cnetworkinfo.zip This little ...

    MATLAB实现K-means聚类

    emptyactNames = {'error','drop','singleton'}; i = strmatch(lower(emptyact), emptyactNames); if length(i) > 1 error(sprintf('Ambiguous ''emptyaction'' parameter value: %s.', emptyact)); elseif ...

    C# Game Programming Cookbook for Unity 3D - 2014

    Acknowledgments xiii Introduction xv 1. Making Games the Modular Way 1 1.1 Important Programming Concepts.....................................2 1.1.1 Manager and Controller Scripts.......................

    21天学习SQL V1.0

    21天学习SQL V1.0.pdf 66 SQL 21 日自学通(V1.0) 翻译人笨猪 ...日期/时间函数............................................................................................................ADD_MONTHS..................

    springmybatis

    MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis ...

    计算机网络第六版答案

    Furthermore, Trudy can even drop the packets that are being sent by Bob to Alice (and vise-versa), even if the packets from Bob to Alice are encrypted. Chapter 1 Problems Problem 1 There...

Global site tag (gtag.js) - Google Analytics