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