从Oracle9i开始,主键创建时的索引和唯一性约束可以在建表时独立定义。
随后,约束可以被独立drop,而索引可以保留。这是Oracle9i中对于索引增强的几个特性之一。
SQL> CREATE TABLE employees
2 (
3 empno NUMBER(6),
4 NAME VARCHAR2(30),
5 dept_no NUMBER(2),
6 CONSTRAINT emp_pk PRIMARY KEY(empno)
7 USING INDEX
8 (CREATE UNIQUE INDEX emp_pk_idx ON employees(empno))
9 );
Table created.
SQL> select index_name,UNIQUENESS from user_indexes
2 where table_name=upper('employees');
INDEX_NAME UNIQUENES
------------------------------ ---------
EMP_PK_IDX UNIQUE
SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,INDEX_NAME from user_constraints
2 where table_name=upper('employees');
CONSTRAINT_NAME C INDEX_NAME
------------------------------ - ------------------------------
EMP_PK P EMP_PK_IDX
Oracle9i中新增的命令可以用以DROP约束保留索引.
ALTER TABLE employees DROP PRIMARY KEY KEEP INDEX;
SQL> ALTER TABLE employees DROP PRIMARY KEY KEEP INDEX;
Table altered.
SQL> select index_name,UNIQUENESS from user_indexes
2 where table_name=upper('employees');
INDEX_NAME UNIQUENES
------------------------------ ---------
EMP_PK_IDX UNIQUE
对于隐式创建的主键索引,同样可以使用此种方式保留:
SQL> CREATE TABLE employees
2 (
3 empno NUMBER(6) primary key,
4 NAME VARCHAR2(30),
5 dept_no NUMBER(2)
6 );
Table created.
SQL> ALTER TABLE employees DROP PRIMARY KEY KEEP INDEX;
Table altered.
SQL> select index_name,UNIQUENESS from user_indexes
2 where table_name=upper('employees');
INDEX_NAME UNIQUENES
------------------------------ ---------
SYS_C004180 UNIQUE
原文地址:http://www.eygle.com/archives/2006/02/oracle9i_enhancement_keep_index.Html