在 Oracle 中使用 nvl 创建唯一约束
当我需要将空值视为相等时,如何创建唯一约束。
为了
修改表T1添加约束T1_UN 唯一 (nvl(C1, ' '))
我在 nvl 上获得
ORA-00904: : invalid identifier
积分
谢谢!
How can I create unique constraint when I need to treat null values as equals.
For
alter table T1 add constraint T1_UN
unique (nvl(C1, ' '))
i get
ORA-00904: : invalid identifier
points on nvl
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NOT NULL
似乎是一个更好的主意,但您可以使基于函数的索引唯一:NOT NULL
seems like a better idea, but you could make a function-based index unique:只需将列设为
NOT NULL
即可。NULL
的本质是它永远不等于任何东西。因此,列中的每个NULL
值在某种程度上已经是UNIQUE
了。像您想要的那样将它们包含在UNIQUE
键中是没有意义的。您想要这样做的原因可能是现有的数据完整性问题。因此,要使列NOT NULL
,您可能必须首先将NULL
值迁移到其他内容...注意:您可以简单地使列
UNIQUE
。然后你可以有几个NULL
值,如上所述......Just make the column
NOT NULL
.The nature of
NULL
is that it is never equal to anything. Hence everyNULL
value in your column is alreadyUNIQUE
in a way. It doesn't make sense to include them in aUNIQUE
key like you want to. The reason why you want to do it is probably an existing data integrity problem. So to make the columnNOT NULL
, you might have to migrateNULL
values to something else, first...Note: You can make the column just simply
UNIQUE
. Then you can have severalNULL
values as described above...