[每日一题] OCP1z0-047 :2013-08-14 如何理解USING INDEX?...................................41

时间:2021-11-02 09:37:37

[每日一题] OCP1z0-047 :2013-08-14 如何理解USING INDEX?...................................41

[每日一题] OCP1z0-047 :2013-08-14 如何理解USING INDEX?...................................41

正确答案:B

一、USING INDEX的实验:

、USING INDEX可以让你在创建主键、唯一性约束的时候使用指定的索引或创建索引、或修改索引的存储结构。

OK,我先不用USING INDEX,创建主键时Oracle自动创建唯一索引。

gyj@MYDB> alter table emp add constraint emp_id_pk primary key(employee_id);

Table altered.

gyj@MYDB> select INDEX_NAME from user_constraints where CONSTRAINT_NAME='EMP_ID_PK';

INDEX_NAME
------------------------------
EMP_ID_PK gyj@MYDB> select UNIQUENESS from user_indexes where index_name='EMP_ID_PK'; UNIQUENES
---------
UNIQUE

、这时我想把主键删除,但我想保留唯一索引EMP_ID_PK,即约束可以被独立drop,而索引可以保留。

gyj@MYDB> ALTER TABLE emp DROP PRIMARY KEY KEEP INDEX;

Table altered.

gyj@MYDB> select UNIQUENESS from user_indexes where index_name='EMP_ID_PK';

UNIQUENES
---------
UNIQUE gyj@MYDB> select INDEX_NAME from user_constraints where CONSTRAINT_NAME='EMP_ID_PK'; no rows selected

、然后我又想创建主键,但我想直接用刚刚创建的唯一索引EMP_ID_PK。此时我就要用USING INDEX。

gyj@MYDB> alter table emp add constraint emp_id_pk primary key(employee_id) using index EMP_ID_PK;

Table altered.

二、      USING INDEX在官方文的解释:

Using Indexes to Enforce Constraints

When defining the state of a unique or primary key constraint, you can specify an index for Oracle to use to enforce the constraint, or you can instruct Oracle to create the index used to enforce the constraint.

using_index_clauseYou can specify the using_index_clause only when enabling unique or primary key constraints. You can specify the clauses of theusing_index_clause in any order, but you can specify each clause only once.

  • If you specify schema.index, then Oracle attempts to enforce the constraint using the specified index. If Oracle cannot find the index or cannot use the index to enforce the constraint, then Oracle returns an error.

  • If you specify the create_index_statement, then Oracle attempts to create the index and use it to enforce the constraint. If Oracle cannot create the index or cannot use the index to enforce the constraint, then Oracle returns an error.

  • If you neither specify an existing index nor create a new index, then Oracle creates the index. In this case:

    • The index receives the same name as the constraint.

    • If table is partitioned, then you can specify a locally or globally partitioned index for the unique or primary key constraint.

Restrictions on theusing_index_clause The following restrictions apply to theusing_index_clause:

  • You cannot specify this clause for a view constraint.

  • You cannot specify this clause for a NOT NULL, foreign key, or check constraint.

  • You cannot specify an index (schema.index) or create an index (create_index_statement) when enabling the primary key of an index-organized table.

  • You cannot specify the domain_index_clause of index_properties or theparallel_clause of index_attributes.