Create unique constraint with nvl in Oracle

北城余情 提交于 2020-01-15 11:25:48

问题


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!


回答1:


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, ' '));



回答2:


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...



来源:https://stackoverflow.com/questions/6369613/create-unique-constraint-with-nvl-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!