How to do unique constraint works with NULL value in MySQL

后端 未结 2 431
执念已碎
执念已碎 2020-12-16 07:51

I am looking for how to implement unique constraints with NULL check.

MySQL shouldn\'t allow multiple null value.

Employee:

id | name
---|---         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 08:20

    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.

提交回复
热议问题