Oracle主键、唯一键与唯一索引的区别
2015-09-16 20:13
656 查看
Oracle主键、唯一键与唯一索引的区别
一般,我们看到术语“索引”和“键”交换使用,但实际上这两个是不同的。索引是存储在数据库中的一个物理结构,键纯粹是一个逻辑概念。键代表创建来实施业务规则的完整性约束。索引和键的混淆通常是由于数据库使用索引来实施完整性约束。
接下来我们看看数据库中的主键约束、唯一键约束和唯一索引的区别。
伦理片 http://www.dotdy.com/
在test表中,我们指定了ID列作为主键,Oracle数据库会自动创建一个同名的唯一索引:
?
此时,如果我们再试图在ID列上创建一个唯一索引,Oracle会报错,因为该列上已经存在一个唯一索引:
?
即使创建非唯一索引也不行:
?
那么唯一键约束的情况是怎样的呢?
?
查看此时的索引情况:
?
Oracle同样自动创建了一个同名的唯一索引,而且也不允许再在此列上创建唯一索引或非唯一索引。
我们知道,主键约束要求列值非空(NOT NULL),那么唯一键约束是否也要求非空呢?
?
从实验结果来看,唯一键约束并没有非空要求。
接下来我们看看唯一索引对列值的非空要求有什么不同。
?
通过实验,我们看出唯一索引与唯一键约束一样对列值非空不做要求。
如果我们让主键约束或者唯一键约束失效,Oracle自动创建的唯一索引是否会受到影响?
?
当主键约束或者唯一键约束失效时,Oracle会标记隐式创建的唯一索引为删除状态。
=====================================================
Using Nonunique Indexes to Enforce Uniqueness
You can use an existing nonunique index on a table to enforce uniqueness, either for UNIQUE constraints or the unique aspect
of a PRIMARY KEY constraint. The advantage of this approach is that the index remains available and valid when the constraint is disabled. Therefore, enabling a disabled UNIQUE or PRIMARY KEY constraint does not require rebuilding the unique index associated
with the constraint. This can yield significant time savings on enable operations for large tables.
Using a nonunique index to enforce uniqueness also lets you eliminate redundant indexes. You do not need a unique index on a primary key column if that column is included as the prefix of a composite index. You can use the existing index to enable and
enforce the constraint. You also save significant space by not duplicating the index. However, if the existing index is partitioned, then the partitioning key of the index must also be a subset of the UNIQUE key; otherwise, Oracle Database
creates an additional unique index to enforce the constraint.
=====================================================
如果我们先创建唯一索引,再创建主键或者唯一键约束,情况又会怎样呢?
?
总结如下:
(1)主键约束和唯一键约束均会隐式创建同名的唯一索引,当主键约束或者唯一键约束失效时,隐式创建的唯一索引会被删除;
(2)主键约束要求列值非空,而唯一键约束和唯一索引不要求列值非空;
(3)相同字段序列不允许重复创建索引;
一般,我们看到术语“索引”和“键”交换使用,但实际上这两个是不同的。索引是存储在数据库中的一个物理结构,键纯粹是一个逻辑概念。键代表创建来实施业务规则的完整性约束。索引和键的混淆通常是由于数据库使用索引来实施完整性约束。
接下来我们看看数据库中的主键约束、唯一键约束和唯一索引的区别。
伦理片 http://www.dotdy.com/
在test表中,我们指定了ID列作为主键,Oracle数据库会自动创建一个同名的唯一索引:
?
此时,如果我们再试图在ID列上创建一个唯一索引,Oracle会报错,因为该列上已经存在一个唯一索引:
?
即使创建非唯一索引也不行:
?
那么唯一键约束的情况是怎样的呢?
?
查看此时的索引情况:
?
Oracle同样自动创建了一个同名的唯一索引,而且也不允许再在此列上创建唯一索引或非唯一索引。
我们知道,主键约束要求列值非空(NOT NULL),那么唯一键约束是否也要求非空呢?
?
从实验结果来看,唯一键约束并没有非空要求。
接下来我们看看唯一索引对列值的非空要求有什么不同。
?
通过实验,我们看出唯一索引与唯一键约束一样对列值非空不做要求。
如果我们让主键约束或者唯一键约束失效,Oracle自动创建的唯一索引是否会受到影响?
?
当主键约束或者唯一键约束失效时,Oracle会标记隐式创建的唯一索引为删除状态。
=====================================================
Using Nonunique Indexes to Enforce Uniqueness
You can use an existing nonunique index on a table to enforce uniqueness, either for UNIQUE constraints or the unique aspect
of a PRIMARY KEY constraint. The advantage of this approach is that the index remains available and valid when the constraint is disabled. Therefore, enabling a disabled UNIQUE or PRIMARY KEY constraint does not require rebuilding the unique index associated
with the constraint. This can yield significant time savings on enable operations for large tables.
Using a nonunique index to enforce uniqueness also lets you eliminate redundant indexes. You do not need a unique index on a primary key column if that column is included as the prefix of a composite index. You can use the existing index to enable and
enforce the constraint. You also save significant space by not duplicating the index. However, if the existing index is partitioned, then the partitioning key of the index must also be a subset of the UNIQUE key; otherwise, Oracle Database
creates an additional unique index to enforce the constraint.
=====================================================
如果我们先创建唯一索引,再创建主键或者唯一键约束,情况又会怎样呢?
?
总结如下:
(1)主键约束和唯一键约束均会隐式创建同名的唯一索引,当主键约束或者唯一键约束失效时,隐式创建的唯一索引会被删除;
(2)主键约束要求列值非空,而唯一键约束和唯一索引不要求列值非空;
(3)相同字段序列不允许重复创建索引;
相关文章推荐
- Oracle环境下的Hibernate方言配置
- 利用ORACLE实现数据抽样(sample block)
- oracle数据库实例启动与关闭
- oracle 表空间不足处理
- Kettle 5.4 SQL2Oracle简单实例
- Oracle 学习笔记 18 -- 存储函数和存储过程(PL/SQL子程序)
- window、linux安装jdk,excel 导入oracle,WebService,window 端口查看,svn服务安装,oracle用户解锁
- Oracle临时表 和 插入多条语句 用法
- oracle海量数据中提升创建索引的速度
- Oracle或者PostgreSQL的row_number over 排名语法
- maven添加oracle jdbc依赖
- Oracle数据库批量插入使用Sequence.nextval报错ORA:02287问题解决
- Oracle 如何查询被锁定表及如何解锁释放session
- Oracle 实例几个function
- Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字
- 一个非常标准的Java连接Oracle数据库的示例代码
- Oracle数据库SQL语句大全
- oracle数据导入导出表等系列操作总结
- ORACLE自定义函数——10进制转36进制
- Oracle timestamp类型介绍及运用