There are many occasions where you need to INSERT a record into a MySQL database table, but the record already exists.

INSERT INTO queue_domains (domain) VALUES('')

This will result in a Duplicate entry '' for key 'domain' error

A nice and simple way to resolve this is to use REPLACE INTO instead.

This will look to see if the item already exists and will simply replace it.

This is faster than doing a SELECT followed by an INSERT.

If you have an auto_increment column set up, then it will delete the previous record and recreate it with a new auto_increment_id.

How to write the new syntax

Just replace the INSERT INTO with REPLACE INTO and you’re done.

So the above failing SQL statement will become:

REPLACE INTO queue_domains (domain) VALUES('')