This article mainly introduces you to the relevant information about unique constraints and NULL in MySQL. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it can take a look below. Hope it helps everyone.
Preface
A requirement I made before, a simplified description is to accept MQ messages from other groups, and then insert a record into the database. In order to prevent them from sending repeated messages and inserting multiple duplicate records, unique indexes were added to several columns in the table.
CREATE UNIQUE INDEX IDX_UN_LOAN_PLAN_APP ON testTable (A, B, C);
At this time, the three columns A, B, and C do not allow NULL values, and the unique constraint also works.
Later, due to changes in requirements, the previous uniqueness constraint was modified and an additional column was added. (I won’t go into details as to why.)
ALTER TABLE testTable DROP INDEX IDX_UN_LOAN_PLAN_APP, ADD UNIQUE KEY `IDX_UN_LOAN_PLAN_APP` (A, B, C, D);
The newly added D is of type datetime, allowing NULL, and the default value is NULL. The reason why the default value is NULL is because not all records have this time. If you forcibly set a Magic Value (such as '1970-01-01 08:00:00') as the default value, it will look strange.
Blue Queen. . . Something went wrong. After adding D, the uniqueness constraint is basically invalid.
Insert into testTable (A,B,C,D) VALUES (1,2,3,NULL); --- OK Insert into testTable (A,B,C,D) VALUES (1,2,3,NULL); --- OK Insert into testTable (A,B,C,D) VALUES (1,2,3,NULL); --- OK
The above three SQLs can all be executed successfully, and there will be multiple identical records in the database. According to our previous idea, the 'Duplicate key' exception should be thrown when executing the last two SQLs.
After checking, I found out that the MySQL official document has clearly stated this. The unique index allows the existence of multiple NULL values:
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.
From the following table, It can be seen that no matter what type of storage engine is used, multiple NULLs are allowed to exist when creating a unique key. . . .
If you think about it carefully, it is actually quite reasonable. After all, NULL is considered to represent "unknown" in MySQL. In SQL, the comparison between any value and NULL returns NULL instead of TRUE, even the comparison between NULL and NULL returns NULL.
So we can only fix it. . . The solution is quite simple and crude. Just refresh the online data, set "1970-01-01 08:00:00" as the default value, and then change that column to not allow NULL, ahem.
Many people have discussed this issue on the MySQL official website. Some people think it is a bug of MySQL, while others think it is a feature. A link is attached.
MySQL Bugs: #8173: unique index allows duplicates with null values
Related recommendations:
Detailed explanation of constraints, multi-table queries and subqueries in MySQL
Sharing of code ideas for type constraints in php
The above is the detailed content of Detailed explanation of unique constraints and NULL instances in MySQL. For more information, please follow other related articles on the PHP Chinese website!