在 Oracle 中使用 nvl 创建唯一约束

发布于 2024-11-15 14:28:36 字数 213 浏览 8 评论 0原文

当我需要将空值视为相等时,如何创建唯一约束。

为了

修改表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-11-22 14:28:36

NOT NULL 似乎是一个更好的主意,但您可以使基于函数的索引唯一:

 create unique index idx_t1 on t1 (nvl(C1, ' '));

NOT NULL seems like a better idea, but you could make a function-based index unique:

 create unique index idx_t1 on t1 (nvl(C1, ' '));
通知家属抬走 2024-11-22 14:28:36

只需将列设为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 every NULL value in your column is already UNIQUE in a way. It doesn't make sense to include them in a UNIQUE key like you want to. The reason why you want to do it is probably an existing data integrity problem. So to make the column NOT NULL, you might have to migrate NULL values to something else, first...

Note: You can make the column just simply UNIQUE. Then you can have several NULL values as described above...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文