你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle 主键、唯一键与唯一索引的区别

[日期:2013-09-19] 来源:Linux社区  作者:Med1tat1on [字体:大 中 小]

实验结果表明,先创建的唯一索引不受约束失效的影响。

SCOTT@ orcl> create index idx_test_id on test(id);
Index created.

SCOTT@ orcl> alter table test add constraint uk_test unique (id);
Table altered.

SCOTT@ orcl> insert into test values(1, 'liu');
insert into test values(1, 'liu')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_TEST) violated

SCOTT@ orcl> alter table test drop constraint uk_test;
Table altered.

SCOTT@ orcl> insert into test values(1, 'liu');
1 row created.

SCOTT@ orcl> delete from test where id=1 and rownum=1;
1 row deleted.

SCOTT@ orcl> select * from test;
ID NAME
---------- --------------------
yang
1 liu

SCOTT@ orcl> create unique index idx_test_id on test(id);
Index created.

SCOTT@ orcl> alter table test add constraint uk_test unique (id);
Table altered.

SCOTT@ orcl> alter table test drop constraint uk_test;
Table altered.

SCOTT@ orcl> insert into test values(1, 'liu');
insert into test values(1, 'liu')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.IDX_TEST_ID) violated

总结如下:
(1)主键约束和唯一键约束均会隐式创建同名的唯一索引,当主键约束或者唯一键约束失效时,隐式创建的唯一索引会被删除;
(2)主键约束要求列值非空,而唯一键约束和唯一索引不要求列值非空;
(3)相同字段序列不允许重复创建索引;

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款