How to do unique constraint works with NULL value in MySQL

No, MySQL is doing the right thing, according to the SQL-99 specification.

https://mariadb.com/kb/en/sql-99/constraint_type-unique-constraint/

A UNIQUE Constraint makes it impossible to COMMIT any operation that
would cause the unique key to contain any non-null duplicate values.
(Multiple null values are allowed, since the null value is never equal
to anything, even another null value.)

If you use a UNIQUE constraint but don’t want multiple rows with NULL, declare the columns as NOT NULL and prohibit any row from having NULL.

Leave a Comment