MySQL Insert If Not Exists
INSERT IGNORE is the syntax for something equivalent for MySQL INSERT IF NOT EXISTS. There is really no direct command matching the SQL Server version as outlined in this articles title, at least not in the current release of MySql.
The statement INSERT IGNORE and to some extent REPLACE INTO, do essentially the same thing, inserting a record if that given record does not exists.
MySQL insert if not exists
Let’s consider the following samples, where we want a record inserted if it’s not there already and ignoring the insert if it exists, vs updating the record if it exists in the second example.
Insert only if that record does not already exist
INSERT IGNORE INTO `table` SET 'column_1` = `value_1`, `column_2` = `value_2';
If you’re concerned with existing data that might result in duplicate records, or error out due to duplicate id’s, use REPLACE. MySQL will check if the record exists, replace the values given and insert it if the record does not exist.
Insert if the record does not exist, otherwise update record
REPLACE INTO `table` SET 'column_1` = `value_1`, `column_2` = `value_2';