Using INSERT IGNORE and ON DUPLICATE KEY UPDATE With MySQL to Prevent Duplicate Key Issue

When you insert multiple rows in mysql database and want to skip duplicate entries. Otherwise you always get mysql failure message of duplicate key.

To overcome this issue Mysql provide two type of Query for insert data. First one is INSERT IGNORE and other is ON DUPLICATE KEY UPDATE.

Lets check it how we can use these Mysql Query:

Insert Ignore Mysql Query :

When you use Insert Ignore query instead of Insert command while inserting too many rows data. If any duplicate row entry exist, than its silently discard the MYSQL function without show any error.

Syntax:

On Duplicate Key Update Mysql Query :

Using this mysql query while insert rows data in database table, find any duplicate entry row, Update that row with the data. Basically this query is when you want to change the data on result of a duplicate key.

It will insert sql statement data when get duplicate key in table without show any error.

Syntax:

Using both above MySQL query you can prevent Duplicate Key error. As per performance basis i recommended to use INSERT IGNORE way while insert too many rows data. Its execute faster and omit the duplicate entry without any replacement in data.

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish

Leave a Comment

Your email address will not be published. Required fields are marked *