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?
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?
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 :
NULL and NULL is not allowedNULL and NULL is fine, but StackOverflow and StackOverflow is not allowedSince 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