I have a mysql table with a auto incremented key. I need a way to only insert into that table if the table does not contain the row I am inserting. INSERT IGNORE and INSERT ON DUPLICATE KEY UPDATE won’t work because the auto incremented key will cause the row to always be different. What is another way I can insert the following line only if there is no duplicate row? Thanks.
INSERT INTO TableName (column1, column2, column3) VALUES ("value1", "value2", "value3");
Set a
UNIQUEconstraint on whichever column you need to be unique, or a combination of columns.For example:
If you want to ignore any error a duplicate insert might give, use
INSERT IGNORE, although you may want to catch this error instead of brushing it under the rug.