Problem solve Get help with specific problems with your technologies, process and projects.

Multiple unique NULLs, revisited

I was reading your answer to a question about unique index in a column with null values. You answered:

"As to your question about whether a UNIQUE column can have more than one NULL, I'm certain the answer is No. NULLs are not just another value, they do have a special interpretation, but if the column is UNIQUE (and assuming it allows NULLs), only one row can have a NULL value".

My logic agrees with you. But I was able to create unique index in a column and then insert multiple null values in the column without any complaint from ORACLE. Do you have any explanation?

Do I have an explanation? Yes, and a simple one, too: I am an idiotTM©®.

Actually, I was also alerted to my error by Lars Berg of Mimer SQL, who emailed me to point out that according to the SQL standard, multiple NULL values are indeed allowed in a column with a UNIQUE index. It seems counter-intuitive, but there you go. Not every database will support this, however. Microsoft SQL/Server, for example, says Multiple NULL values are considered duplicates when UNIQUE index is created. So it looks like Oracle gets it right, at least according to the SQL standard.

Mr. Berg also said

Indexes are not defined in the ANSI documents but X/Open have included support for indexes in their CAE Specification SQL version 2. This specification decrees that a unique index may only contain one value that is null. So this is contrary to ANSI sql and may be the reason why some manufacturers have the same behaviour for unique constraints.

My thanks to you and to Mr. Berg for spotting my error.

Dig Deeper on Oracle and SQL

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.