Using INSERT IGNORE with MySQL to prevent duplicate key errors
Posted in MySql - Last updated Feb. 18, 2019
An error will occur when inserting a new record in MySQL if the primary key specified in the insert query already exists. Using the "IGNORE" keyword prevents errors from occuring and other queries are still able to be run.
Why?
Although you shouldn't normally attempt to insert a record without first checking if the primary key you want to use has already been used, there may be times when this is required, such as when multiple developers need to update their own copies of a database, and a particular record may already exist in one or other of the databases.
Inserting a single record
The syntax is simple - just add "IGNORE" after "INSERT" like so: