oracle的约束隐式建立索引和先索引后约束的区别oracle
两种状况:
1.对于建立约束时隐式建立的索引,在作删除操做的时候: 9i~11g都会连带删除该索引索引
2.对于先建立索引,再建立约束(使用到此索引)这种状况:
9i版本:须要区分索引是否惟一:
若是索引是惟一的,则删除约束的时候,会连带删除索引;若是非惟一的,则不会删除索引。
10g之后版本,包括11g:不管索引是否惟一,都只是删除约束,索引不会删除。 文档
参考metalink文档:309821.1io
实验验证下
$ sstable
SQL*Plus: Release 11.2.0.3.0 Production on Wed Apr 1 18:29:31 2015test
Copyright (c) 1982, 2011, Oracle. All rights reserved.select
Connected to an idle instance.meta
SQL> startup
ORACLE instance started.im
Total System Global Area 889389056 bytes
Fixed Size 2233480 bytes
Variable Size 830475128 bytes
Database Buffers 50331648 bytes
Redo Buffers 6348800 bytes
Database mounted.
Database opened.
SQL>
SQL>
SQL>
SQL> conn hr/hr
Connected.tab
先建立的索引,不管是不是unique索引,都不会随约束删除而被删除
SQL> create table test(a number );
Table created.
SQL> create index ind on test ( a );
Index created.
SQL> alter table test add constraint c1_pk primary key(a) using index;
Table altered.
SQL> select index_name from user_indexes where table_name='TEST';
INDEX_NAME
------------------------------
IND
SQL> alter table test drop constraint c1_pk;
Table altered.
SQL> select index_name from user_indexes where table_name='TEST';
INDEX_NAME
------------------------------
IND
SQL> drop index IND ;
Index dropped.
SQL> create unique index ind2 on test ( a );
Index created.
SQL> alter table test add constraint c2_pk primary key(a) using index;
Table altered.
SQL> alter table test drop constraint c2_pk;
Table altered.
SQL> select index_name from user_indexes where table_name='TEST';
INDEX_NAME
------------------------------
IND2
清理一下环境,删除索引,而后直接建约束,隐式建立索引,索引会由于约束被删除,而同时被删除
SQL> drop index ind2 ;
Index dropped.
SQL>
SQL>
SQL> alter table test add constraint c2_pk primary key(a) using index;
Table altered.
SQL> select index_name from user_indexes where table_name='TEST';
INDEX_NAME
------------------------------
C2_PK
SQL> alter table test drop constraint c2_pk;
Table altered.
SQL> select index_name from user_indexes where table_name='TEST';
no rows selected