Oracle中唯一约束和唯一索引的区别(转)

约束和索引, 前者是用来检查数据的正确性,后者用来实现数据查询的优化,目的不同。

创建唯一约束与创建唯一索引有所不同:

  • 创建唯一约束会在Oracle中创建一个Constraint,同时也会创建一个该约束对应的唯一索引。
  • 创建唯一索引只会创建一个唯一索引,不会创建Constraint。

也 就是说其实唯一约束是通过创建唯一索引来实现的。

在删除时这两者也有一定的区 别:

删除唯一约束时可以只删除约束而不删除对应的索引,所以对应的列还是必须唯一的,

而删除了唯一索引的话就可以插入不唯一的值。

另:

UNIQUE Constraints vs UNIQUE Indexes

Many database administrators ask about the difference between a UNIQUE constraint and a UNIQUE index. While you may use different Transact-SQL commands to create them (ALTER TABLE…ADD CONSTRAINT for constraints and CREATE UNIQUE INDEX for indexes), they have the same effect, for the most part. In fact, when you create a UNIQUE constraint, it actually creates a UNIQUE index on the table.It is significant to note, however, that there are several differences:
When you create an index, you may add additional options to the creation command A column subject to a UNIQUE constraint may be used as a foreign key

MSDN:尽管唯一索引有助于定位信息,但为获得最佳性能结果,建议改用主键或唯一约束。

参考:http://www.cnblogs.com/studyzy/archive/2010/08/23/1806806.html

posted @ 2011-08-17 15:19  peter cheng  阅读(3087)  评论(1编辑  收藏  举报