ChatGPT解决这个技术问题 Extra ChatGPT

Does MySQL ignore null values on unique constraints?

I have an email column that I want to be unique. But I also want it to accept null values. Can my database have 2 null emails that way?


J
Jon Schneider

Yes, MySQL allows multiple NULLs in a column with a unique constraint.

CREATE TABLE table1 (x INT NULL UNIQUE);
INSERT table1 VALUES (1);
INSERT table1 VALUES (1);   -- Duplicate entry '1' for key 'x'
INSERT table1 VALUES (NULL);
INSERT table1 VALUES (NULL);
SELECT * FROM table1;

Result:

x
NULL
NULL
1

This is not true for all databases. SQL Server 2005 and older, for example, only allows a single NULL value in a column that has a unique constraint.


excellent comment about how it's true in mysql, but not necessarily in general.
According to SQLite FAQ, behavior is same in MySQL, PostgreSQL, SQLite, Oracle, and Firebird.
Please update your answer. SQLServer 2008+ absolutely allows for it, you simply have to add a WHERE clause... in 2017, nobody should be on an older version than 2008 anyway... stackoverflow.com/questions/767657/…
this little feature has been very difficult to find an answer to that does not require adding a new column to the database or upgrading MySQL on a very old application. I was really looking for a solution like Postgres where I can use COALESCE but it seems the answer is always it not a bug it how it is designed. Not even WHERE column IS NOT NULL seems to fail me as it not supported in my version of MySQL. Anyone know where I could look?
note: this also works also unique indexes that have more columns. So if you want columns a, b and c to be unique you can still have in the table double rows with null, b, c
M
Matthew Flaschen

From the docs:

"a UNIQUE index permits multiple NULL values for columns that can contain NULL"

This applies to all engines but BDB.


BDB is no longer available on current mysql versions (starting with 5.1.12).
My testing seems to show that the Java Derby database v10.13.1.1. similarly allows only one null in a column with a unique index.
b
bluegman991

I am unsure if the author originally was just asking whether or not this allows duplicate values or if there was an implied question here asking, "How to allow duplicate NULL values while using UNIQUE?" Or "How to only allow one UNIQUE NULL value?"

The question has already been answered, yes you can have duplicate NULL values while using the UNIQUE index.

Since I stumbled upon this answer while searching for "how to allow one UNIQUE NULL value." For anyone else who may stumble upon this question while doing the same, the rest of my answer is for you...

In MySQL you can not have one UNIQUE NULL value, however you can have one UNIQUE empty value by inserting with the value of an empty string.

Warning: Numeric and types other than string may default to 0 or another default value.


Constraint has nothing to do with the index. In fact, you won't even be able to have a single row with NULL value despite the fact there is no other such row.
@Pijusn What do you mean by "constraint has nothing to do with index?" Also about your second sentence, I never said that you could have a row with a NULL value, that's why I stated at the beginning of the post, that this is a solution only if he isn't set on using null values.
What I meant is that adding new element fails not because of UNIQUE constraint but because of NOT NULL constraint. I think this answer is irrelevant to the question because the question is specifically about the behaviour of UNIQUE constraint.
@Pijusn I got you. You're right, I have removed the wording suggesting otherwise. I mis-read the question. But I believe the answer still may be useful for users that stumble upon this question as I did while trying to find a way to have a unique "nothing" value, but are mistakenly allowing null-ability.
I found this answer useful. However, it is also answered here. This post was the first result from my google search, though this answer and the linked question were what I was looking for.
n
nvogel

Avoid nullable unique constraints. You can always put the column in a new table, make it non-null and unique and then populate that table only when you have a value for it. This ensures that any key dependency on the column can be correctly enforced and avoids any problems that could be caused by nulls.


Yes, but what you propose is almost exactly what mysql does behind the scenes already. Why re-invent the wheel if this functionality is built in?
Because it's not valid SQL. I believe that this tip will be useful for all who want (or need) a database agnostic design.
@Arsen7 What if you have multiple businesses - each with multiple clients. You store all business with their clients' email addresses in one file. So you can not make email_address unique because different businesses may have the same client. So you have to make a composite unique index of business_id and email_address. Is it possible to put this in a new table - as explained?
I have a case where an "email" column needs to be unique OR null. I'd have to create a new table with a single "email" column if I were to follow your advice. Relying on this Mysql specific behavior is much easier and the result is the same. The customer doesn't care whether I store the email in a new table or not. Also, database agnostic design is all to often overrated. For a lot of projects, you can't and probably wouldn't just switch from one DB to another that easily.
@djmj sure, but functional dependencies are important to most people and the nullable unique constraint version does not enforce the same dependencies as the BCNF version. So which option is more or less practical may depend on which dependencies are important to you. That's why it is worth considering creating a new table.
A
Akshay Katiha

A simple answer would be : No, it doesn't

Explanation : According to the definition of unique constraints (SQL-92)

A unique constraint is satisfied if and only if no two rows in a table have the same non-null values in the unique columns

This statement can have two interpretations as :

No two rows can have same values i.e. NULL and NULL is not allowed

No two non-null rows can have values i.e NULL and NULL is fine, but StackOverflow and StackOverflow is not allowed

Since MySQL follows second interpretation, multiple NULL values are allowed in UNIQUE constraint column. Second, if you would try to understand the concept of NULL in SQL, you will find that two NULL values can't be compared at all since NULL in SQL refers to unavailable or unassigned value (you can't compare nothing with nothing). Now, if you are not allowing multiple NULL values in UNIQUE constraint column, you are contracting the meaning of NULL in SQL. I would summarise my answer by saying :

MySQL supports UNIQUE constraint but not on the cost of ignoring NULL values


There is only one interpretation possible... A unique constraint is satisfied if and only if no two rows in a table have the same ****non-null values**** in the unique columns