主键与索引关系实验

主键与索引关系实验   

增加主键时,如果主键列上存在唯一性索引,则主键直接使用该唯一性索引,如果主键列没有唯一性约束而有非唯一性索引,则使用非唯一性索引来确保主键的唯一特性。

以下为各种情况的详细实验记录:

情况1、当前表不存在索引
 步骤:创建表--&gt插入数据--&gt增加主键--&gt查看该表的索引情况
      
HR@ orcl >create table t(a number,b varchar2(20));
Table created.
HR@ orcl >insert into t values (1,'aaa');
1 row created.
HR@ orcl >insert into t values (2,'bbb');
1 row created.
HR@ orcl >commit;
Commit complete.
HR@ orcl >alter table t add constraint pk_t_a primary key (a);
Table altered.
HR@ orcl >select index_name ,table_name ,uniqueness from user_indexes
  2  where table_name = 'T';
INDEX_NAME                     TABLE_NAME                     UNIQUENES
------------------------------ ------------------------------ ---------
PK_T_A                         T                              UNIQUE
HR@ orcl >insert into t values (2,'bbb');
insert into t values (2,'bbb')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.PK_T_A) violated
HR@ orcl >drop index PK_T_A;
drop index PK_T_A
          *
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key

 注:这种情况在增加主键的时候会自动创建以主键名命名的唯一性索引,说明主键约束需要使用唯一性索引来确保主键的唯一。
情况2、当前表存在唯一性索引
步骤:创建表--&gt创建唯一性索引--&gt增加主键--&gt查看该表的索引情况
HR@ orcl >drop table t;
Table dropped.
HR@ orcl >create table t(a number,b varchar2(20));
Table created.
HR@ orcl >insert into t values (1,'aaa');
1 row created.
HR@ orcl >insert into t values (2,'bbb');
1 row created.
HR@ orcl >commit;
Commit complete.
HR@ orcl >create unique index idx_unq_t_a on t(a);
Index created.
HR@ orcl >alter table t add constraint pk_t_a primary key (a);
Table altered.
HR@ orcl >select index_name ,table_name ,uniqueness from user_indexes
  2  where table_name ='T';
INDEX_NAME                     TABLE_NAME                     UNIQUENES
------------------------------ ------------------------------ ---------
IDX_UNQ_T_A                    T                              UNIQUE
HR@ orcl >insert into t values (2,'bbb');
insert into t values (2,'bbb')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.PK_T_A) violated
HR@ orcl >drop index IDX_UNQ_T_A;
drop index IDX_UNQ_T_A
           *
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key

注:这种情况在创建主键的时候不用指定using index 选项,Oracle会自动查找指定到当前表的唯一性索引。

情况3、当前表不存在唯一性索引,存在非唯一性索引
步骤:创建表--&gt创建非唯一性索引--&gt增加主键--&gt查看该表的索引情况
HR@ orcl >drop table t;
Table dropped.
HR@ orcl >create table t(a number,b varchar2(20));
Table created.
HR@ orcl >insert into t values (1,'aaa');
1 row created.
HR@ orcl >insert into t values (2,'bbb');
1 row created.
HR@ orcl >commit;
Commit complete.
HR@ orcl >create index idx_t_a on t(a); --此处创建的是非唯一性索引
Index created.
HR@ orcl >alter table t add constraint pk_t_a primary key (a);
Table altered.
HR@ orcl >insert into t values (2,'bbb');
insert into t values (2,'bbb')
*
ERROR at line 1:
ORA-00001: unique constraint (HR.PK_T_A) violated
HR@ orcl >select index_name ,table_name ,uniqueness from user_indexes
  2  where table_name ='T';
INDEX_NAME                     TABLE_NAME                     UNIQUENES
------------------------------ ------------------------------ ---------
IDX_T_A                        T                              NONUNIQUE
HR@ orcl >drop index IDX_T_A;--此处删除非唯一性索引时候,报错,提示不能删除。
drop index IDX_T_A
           *
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key
注:这种情况在创建主键的时候,Oracle自动指定了一个非唯一性索引来进行主键约束,检查是否有重复值。

按理说只有唯一性约束才能确保主键的唯一特性,根据现在的实验结果,非唯一索引也能确保主键的唯一特性。以上仅根据实验结果,提出的个人观点,仅供参考,如有错误之处,敬请指出……

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15489979/viewspace-732121/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15489979/viewspace-732121/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值