I have a table with an auto_inc
id (primary key).
I am trying to avoid the insertion of duplicate rows.
Example of a duplicate row:
id | field a | field b | field c |
1 4 6 7
2 4 6 7
The key (id
) is not duplicate since it is automatically generated by MySQL, but all other fields are identical.
Make a unique index on fields a,b,c.
ALTER TABLE `table` ADD UNIQUE (
`a` ,
`b` ,
`c`
);
You should use ON DUPLICATE KEY UPDATE and declaring the fields as unique .
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With