oracle 唯一约束,急急急!!!!!!

liubei611 2010-09-10 12:05:09
各位:
unique索引如果是组合索引,那么允许其中的某个字段为空么?
...全文
410 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaertiger999 2010-09-16
  • 打赏
  • 举报
回复
YY2句换位思考
2条 记录
1 2 null
1 2 null
如果你是ORACLE ,你打算通过什么算法对他们进行定位。
cnwz 2010-09-13
  • 打赏
  • 举报
回复
可以为空
但非空字段(唯一索引组合字段其他非空字段)不能重复(NULL不写入索引,所以只要写入索引部分唯一即可)
如果是单一字段唯一索引,也可以多NULL(原理相同)

要注意的是,如果通过改索引检索,可能会导致全表扫描
nwp123 2010-09-13
  • 打赏
  • 举报
回复
null 值不会索引中, 对unique索引组合索引,只要去掉null 部分,还是唯一就可以了, (在这时你可以想象为null == null)
心中的彩虹 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 liubei611 的回复:]
各位:
unique索引如果是组合索引,那么允许其中的某个字段为空么?
[/Quote]
可以 只要保持唯一就行
gelyon 2010-09-10
  • 打赏
  • 举报
回复
unique索引如果是组合索引,是允许其中的某个字段为空的!
但是由于是unique,那么就必须任何两条记录唯一,可以一条记录的某字段为空,其他记录如果另外的组合unique一样,那么为空的那个字段就不能再是空的了,只要保证唯一就行!
  • 打赏
  • 举报
回复
SQL> create  unique index testidx on test(id,ename);

索引已创建。

SQL> insert into test(id) values(1);

已创建 1 行。

SQL> select * from test;

ID ENAME
---------- --------------------
1
minitoy 2010-09-10
  • 打赏
  • 举报
回复
额,这里居然认为null=null
minitoy 2010-09-10
  • 打赏
  • 举报
回复
SQL> create table t_test_index
2 (a number(10),
3 b number(10),
4 c number(10),
5 d number(10));

Table created

SQL> create unique index t_test_index1 on t_test_index(a,b,c);

Index created

SQL> insert into t_test_index values(null,1,2,3);

1 row inserted

SQL> insert into t_test_index values(null,1,2,3);

insert into t_test_index values(null,1,2,3)

ORA-00001: 违反唯一约束条件 (TDS.T_TEST_INDEX1)

SQL>
minitoy 2010-09-10
  • 打赏
  • 举报
回复
应该是可以的.
加油馒头 2010-09-10
  • 打赏
  • 举报
回复
可以为空,组合索引
minitoy 2010-09-10
  • 打赏
  • 举报
回复
挺有意思.当组合索引各个字段都为null的时候,数据可以重复插入,不违反唯一约束.但是当不都是null的时候不能重复插入.
SQL> truncate table t_test_index;

truncate table t_test_index

ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源

SQL> truncate table t_test_index;

Table truncated

SQL> insert into t_test_index values(null,1,2,3);

1 row inserted

SQL> insert into t_test_index values(null,1,2,3);

insert into t_test_index values(null,1,2,3)

ORA-00001: 违反唯一约束条件 (TDS.T_TEST_INDEX1)

SQL> insert into t_test_index values(null,null,2,3);

1 row inserted

SQL> insert into t_test_index values(null,null,2,3);

insert into t_test_index values(null,null,2,3)

ORA-00001: 违反唯一约束条件 (TDS.T_TEST_INDEX1)

SQL> insert into t_test_index values(null,null,null,3);

1 row inserted

SQL> insert into t_test_index values(null,null,null,3);

1 row inserted

SQL>
Dream_1986 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gelyon 的回复:]

unique索引如果是组合索引,是允许其中的某个字段为空的!
但是由于是unique,那么就必须任何两条记录唯一,可以一条记录的某字段为空,其他记录如果另外的组合unique一样,那么为空的那个字段就不能再是空的了,只要保证唯一就行!
[/Quote]++

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧