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

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/9972521




正确答案:B


一、USING INDEX的实验:

1USING 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


2、这时我想把主键删除,但我想保留唯一索引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

3、然后我又想创建主键,但我想直接用刚刚创建的唯一索引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 theusing_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_clauseThe 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 ofindex_attributes.

QQ:252803295

学习交流QQ群:
DSI&Core Search  Ⅰ 群:127149411(技术:已满)
DSI&Core Search  Ⅱ 群:177089463(技术:未满)
DSI&Core Search  Ⅲ 群:284596437(技术:未满)
DSI&Core Search  Ⅳ 群:192136702(技术:未满)
DSI&Core Search  Ⅴ 群:285030382(闲聊:未满)



MAIL:oracledba_cn@hotmail.com

BLOG: http://blog.csdn.net/guoyjoe

WEIBO:http://weibo.com/guoyJoe0218

ITPUB: http://www.itpub.net/space-uid-28460966.html

OCM:   http://education.oracle.com/education/otn/YGuo.HTM


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值