How to write INSERT IF NOT EXISTS queries in standard SQL

If necessary, INSERT IF NOT EXISTS queries can be written in a single atomic statement, eliminating the need for a transaction, and without violating standards.
In this article we explained several ways to write such queries in a platform-independent way.

Several solutions:

One solution is to use a mutex table

[pastacode lang=”sql” manual=”insert%20into%20urls(url)%0Aselect%20’%2Fblog%2F’%0Afrom%20mutex%0A%20%20%20%20left%20outer%20join%20urls%0A%20%20%20%20%20%20%20%20on%20urls.url%20%3D%20’%2Fblog%2F’%0Awhere%20mutex.i%20%3D%201%20and%20urls.url%20is%20null%3B%0A” message=”Sql Code” highlight=”” provider=”manual”/]

There are more flexible variations on this technique. Suppose there is no unique index on the url column.

If desired, it is possible to insert several values in a single statement by changing the WHERE clause:

[pastacode lang=”sql” manual=”insert%20into%20urls(url)%0Aselect%20’%2Fblog%2F’%0Afrom%20mutex%0A%20%20%20%20left%20outer%20join%20urls%0A%20%20%20%20%20%20%20%20on%20urls.url%20%3D%20’%2Fblog%2F’%0Awhere%20mutex.i%20%3C%205%20and%20urls.url%20is%20null%3B%0A” message=”Sql Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Now suppose the requirements specify up to three duplicate entries in the table, and each insert should add a single row.

It is possible to insert a row at a time while enforcing the requirement with the following query:

[pastacode lang=”sql” manual=”insert%20into%20urls(url)%0Aselect%20’%2Fblog%2F’%0Afrom%20mutex%0A%20%20%20%20left%20outer%20join%20urls%0A%20%20%20%20%20%20%20%20on%20urls.url%20%3D%20’%2Fblog%2F’%0Awhere%20mutex.i%20%3D%201%0Agroup%20by%20urls.url%0Ahaving%20count(*)%20%3C%203%3B%0A” message=”Sql Code” highlight=”” provider=”manual”/]

This query shows the input to the INSERT statement:

[pastacode lang=”sql” manual=”select%20’%2Fblog%2F’%2C%20count(*)%0Afrom%20mutex%0A%20%20%20%20left%20outer%20join%20urls%0A%20%20%20%20%20%20%20%20on%20urls.url%20%3D%20’%2Fblog%2F’%0Awhere%20mutex.i%20%3D%201%0Agroup%20by%20urls.url%3B%0A” message=”Sql Code” highlight=”” provider=”manual”/]

To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality.

Another 3 possible solutions:
INSERT IGNORE,
REPLACE,
INSERT … ON DUPLICATE KEY UPDATE.

Below we’ll examine the three different methods and explain the pros and cons of each in turn.

so you have a firm grasp on how to configure your own statements when providing new or potentially existing data for INSERTION.

Using INSERT IGNORE:

Using INSERT IGNORE effectively causes MySQL to ignore execution errors while attempting to perform INSERT statements.

This means that an INSERT IGNORE statement which contains a duplicate value in a UNIQUE index or PRIMARY KEY field does not produce an error, but will instead simply ignore that particular INSERT command entirely.

The obvious purpose is to execute a large number of INSERT statements for a combination of data that is both already existing in the database as well as new data coming into the system.

For example, our books table might contain a few records already:

mysql> SELECT * FROM books LIMIT 3;

+—-+———————————————————————————————-

| id | title                                  | author                          |  year_published |

+—-+———————————————————————————————

|  1 | In Search of Lost Time   | Marcel Proust             |           1913           |

|  2 | Ulysses                          | James Joyce               |           1922           |

|  3 | Don Quixote                  | Miguel de Cervantes   |           1605           |

+—-+———————————————————————————————-

3 rows in set (0.00 sec)

If we have a large batch of new and existing data to INSERT and part of that data contains a matching value for the id field (which is a UNIQUE PRIMARY_KEY in the table), using a basic INSERT will produce an expected error:

[pastacode lang=”sql” manual=”mysql%3E%20INSERT%20INTO%20books%0A%20%20%20%20(id%2C%20title%2C%20author%2C%20year_published)%0AVALUES%0A%20%20%20%20(1%2C%20’Green%20Eggs%20and%20Ham’%2C%20’Dr.%20Seuss’%2C%201960)%3B%0AERROR%201062%20(23000)%3A%20Duplicate%20entry%20’1’%20for%20key%20’PRIMARY’%0A” message=”Sql Code” highlight=”” provider=”manual”/] [ad type=”banner”]

On the other hand, if we use INSERT IGNORE, the duplication attempt is ignored and no resulting errors occur:

[pastacode lang=”sql” manual=”mysql%3E%20INSERT%20IGNORE%20INTO%20books%0A%20%20%20%20(id%2C%20title%2C%20author%2C%20year_published)%0AVALUES%0A%20%20%20%20(1%2C%20’Green%20Eggs%20and%20Ham’%2C%20’Dr.%20Seuss’%2C%201960)%3B%0AQuery%20OK%2C%200%20rows%20affected%20(0.00%20sec)%0A” message=”Sql Code” highlight=”” provider=”manual”/]

Using REPLACE

In the event that you wish to actually replace rows where INSERT commands would produce errors due to duplicate UNIQUE or PRIMARY KEY values as outlined above, one option is to opt for the REPLACE statement.

When issuing a REPLACE statement, there are two possible outcomes for each issued command:

No existing data row is found with matching values and thus a standard INSERT statement is performed.

A matching data row is found, causing that existing row to be deleted with the standard DELETE statement, then a normal INSERT is performed afterward.

For example,
we can use REPLACE to swap out our existing record of id = 1 of In Search of Lost Time by Marcel Proust with Green Eggs and Ham by Dr. Seuss:

[pastacode lang=”sql” manual=”mysql%3E%20REPLACE%20INTO%20books%0A%20%20%20%20(id%2C%20title%2C%20author%2C%20year_published)%0AVALUES%0A%20%20%20%20(1%2C%20’Green%20Eggs%20and%20Ham’%2C%20’Dr.%20Seuss’%2C%201960)%3B%0AQuery%20OK%2C%202%20rows%20affected%20(0.00%20sec)%0A” message=”Sql Code” highlight=”” provider=”manual”/]

Notice that even though we only altered one row, the result indicates that two rows were affected because, we actually DELETED the existing row then INSERTED the new row to replace it.

Using INSERT … ON DUPLICATE KEY UPDATE

The alternative (and generally preferred) method for INSERTING into rows that may contain duplicate UNIQUE or PRIMARY KEY values is to use the INSERT … ON DUPLICATE KEY UPDATE statement and clause.

Unlike REPLACE – an inherently destructive command due to the DELETE commands it performs when necessary – using INSERT … ON DUPLICATE KEY UPDATE is non-destructive, in that it will only ever issue INSERT or UPDATE statements, but never DELETE.

For example,
we have decided we wish to replace our id = 1 record of Green Eggs and Ham and revert it back to the original In Search of Lost Time record instead.

We can therefore take our original INSERT statement and add the new ON DUPLICATE KEY UPDATE clause:

[pastacode lang=”sql” manual=”mysql%3E%20SET%20%40id%20%3D%201%2C%0A%20%20%20%20%40title%20%3D%20’In%20Search%20of%20Lost%20Time’%2C%0A%20%20%20%20%40author%20%3D%20’Marcel%20Proust’%2C%0A%20%20%20%20%40year_published%20%3D%201913%3B%0AINSERT%20INTO%20books%0A%20%20%20%20(id%2C%20title%2C%20author%2C%20year_published)%0AVALUES%0A%20%20%20%20(%40id%2C%20%40title%2C%20%40author%2C%20%40year_published)%0AON%20DUPLICATE%20KEY%20UPDATE%0A%20%20%20%20title%20%3D%20%40title%2C%0A%20%20%20%20author%20%3D%20%40author%2C%0A%20%20%20%20year_published%20%3D%20%40year_published%3B%0A” message=”Sql Code” highlight=”” provider=”manual”/]

Notice that we’re using normal UPDATE syntax (but excluding the unnecessary table name and SET keyword), and only assigning the non-UNIQUE values.

Also, although unnecessary for the ON DUPLICATE KEY UPDATE method to function properly, we’ve also opted to utilize user variables so we don’t need to specify the actual values we want to INSERT or UPDATE more than once.

As a result, our id = 1 record was properly UPDATED as expected:

mysql> SELECT * FROM books LIMIT 1;

+—-+——————————–+———————+———————+

| id | title                                   | author              | year_published |

+—-+——————————–+——————–+———————-+

|  1 | In Search of Lost Time   | Marcel Proust |           1913          |

+—-+——————————–+——————–+———————–+

1 row in set (0.00 sec)

Categorized in: