How can I create a unique index in Oracle but ignore nulls?

Viewed 38761

I am trying to create a unique constraint on two fields in a table. However, there is a high likelihood that one will be null. I only require that they be unique if both are not null (name will never be null).

create unique index "name_and_email" on user(name, email);

Ignore the semantics of the table and field names and whether that makes sense - I just made some up.

Is there a way to create a unique constraint on these fields that will enforce uniqueness for two not null values, but ignore if there are multiple entries where name is not null and email is null?

This question is for SQL Server, and I'm hoping that the answer is not the same: How do I create a unique constraint that also allows nulls?

2 Answers

I don't know how many people still get directed to this answer, but at least in the latest version of oracle, you're allowed to have multiple rows with null on a unique index and the accepted answer isn't necessary

Related