问题
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