Formerly I was using MyISAM storage engine for MySql and I had defined the combination of three fields to be unique.
Now I have switched to InnoDB, which I assume caused this problem, and now NULL != NULL.
So for the following table:
ID (Auto) | Field_A | Field_B | Field_C
I can insert (Field_A,Field_B,Field_C) Values(1,2,NULL) (1,2,NULL) (1,2,NULL) infinitely many times.
How can I prevent this behavior?
Depends on the business rules, but the first idea would be to set
field_aandfield_bas the primary key for the table. An AUTO_INCREMENT column can be used for a non-primary key column, but the key attribute has to be associated with the auto_increment column:The other alternative is to add a unique constraint (MySQL implements them as an index):