关于唯一键(Unique)
来源:互联网 发布:域名系统安全防护要求 编辑:程序博客网 时间:2024/06/06 15:34
SQL> create table test_ranbo(id number);
Table created
SQL> alter table test_ranbo add constraint test_ranbo$uk unique (id);
Table altered
SQL> insert into test_ranbo(id) values(null);
1 row inserted
SQL> insert into test_ranbo(id) values(null);
1 row inserted
可以插入两个null.
下面建个包含两列外键的例子。
SQL> truncate table test_Ranbo;
Table truncated
SQL> alter table test_ranbo drop constraint test_ranbo$uk;
Table altered
SQL> alter table test_ranbo add Name varchar2(30);
Table altered
SQL> desc test_ranbo;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
ID NUMBER Y
NAME VARCHAR2(30) Y
SQL> alter table test_ranbo add constraint test_ranbo$uk unique (id ,name);
Table altered
SQL> insert into test_ranbo values(1,null);
1 row inserted
SQL> insert into test_ranbo values(1,null);
insert into test_ranbo values(1,null)
ORA-00001: unique constraint (DBOWN.TEST_RANBO$UK) violated
再由测试外键是三列的情况。
SQL> truncate table test_ranbo;
Table truncated
SQL> alter table test_ranbo drop constraint test_ranbo$uk;
Table altered
SQL> alter table test_ranbo add type varchar2(30);
Table altered
SQL> desc test_ranbo;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
ID NUMBER Y
NAME VARCHAR2(30) Y
TYPE VARCHAR2(30) Y
SQL> alter table test_ranbo add constraint test_ranbo$uk unique (id,name,type);
Table altered
SQL> insert into test_ranbo(id,name,type) values(1,null,null);
1 row inserted
SQL> /
insert into test_ranbo(id,name,type) values(1,null,null)
ORA-00001: unique constraint (DBOWN.TEST_RANBO$UK) violated
SQL> insert into test_ranbo(id,name,type) values(null,null,null);
1 row inserted
SQL> /
1 row inserted
SQL> insert into test_ranbo(id,name,type) values(null,'a',null);
1 row inserted
SQL> /
insert into test_ranbo(id,name,type) values(null,'a',null)
ORA-00001: unique constraint (DBOWN.TEST_RANBO$UK) violated
由此可见,在唯一键包含两列以上的情况下,只要有了一列是非null,那么其他列的null可以理解为一个确定的值,唯一键会起作用,只有唯一键的所有列都是null值的时候不管插入多少行相同的记录唯一键都不起作用。
from:http://space.itpub.net/13387766/viewspace-608824
- 关于唯一键(Unique)
- sql UNIQUE 约束唯一
- mysql 字段唯一 UNIQUE
- NULL UNIQUE唯一约束
- mysql的唯一索引UNIQUE
- 生成唯一序列号 Unique ID
- mysql唯一索引和unique
- mysql的唯一索引UNIQUE
- 唯一索引、主键、Unique约束
- 创建unique时,约束和索引有何区别。唯一约束和唯一索引区别,选项"忽略重复键"作用
- 创建unique时,约束和索引有何区别。唯一约束和唯一索引区别,选项"忽略重复键"作用
- 19.6.1 Partitioning Keys, Primary Keys, and Unique Keys 分区键,主键,和唯一健
- hibernate 批量保存数据时存在唯一键unique值重复时报错的解决方式
- SQL约束(主键约束 primary key、外键约束 foreign key、唯一约束unique 、CHECK约束)
- unique-----返回唯一值并排序------------matlab
- UNIQUE,唯一约束,不能往数据库
- 变异算法之唯一(unique, unique_copy)
- 主键与唯一索引(unique index)
- No schema found for … extension point 问题
- 不要纠缠于某些细节,在某个时间只集中做某一项要做的事;有所侧重,而不是淹没而迷失。
- ff和ie处理ajax加载的不同
- PHP将小写改成大写金额
- 寻人启事!!大家帮忙分享。。。急啊
- 关于唯一键(Unique)
- vim使用技巧
- 在Eclipse中使用Remote Debug对Tomcat进行调试
- Javascript:scrollWidth,clientWidth,offsetWidth的区别
- 举例策划JDBC连接数据库源代码
- 人生
- 读人间感言
- ubuntu 10.04 修改锐捷客户端校园网上网 解决
- web.config文件中的特殊字符处理