Insert into a MySQL table or update if exists

Posted by Kyle Hankinson May 1, 2023


In the dynamic world of database management, MySQL stands out for its versatility and robustness. A common requirement for database administrators and developers is to insert a record into a table or update it if it already exists. This task, commonly known as an "upsert", can be efficiently handled in MySQL. This article delves into how to execute an 'Insert or Update' operation without relying on command line tools.

Understanding the Basics

Before diving into the 'upsert' operation, it's crucial to understand two key SQL statements: INSERT and UPDATE. The INSERT statement adds new records to a table, while UPDATE modifies existing records.

The Challenge of 'Upsert'

The 'upsert' operation is a hybrid that combines the functionalities of INSERT and UPDATE. It inserts a new record if it doesn't exist or updates the existing record if it does. The challenge lies in efficiently determining whether to insert or update, especially in tables with a significant amount of data.


MySQL provides a straightforward way to handle 'upserts' using the INSERT ... ON DUPLICATE KEY UPDATE statement. This method requires a unique index or primary key in the table to determine uniqueness.


INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
ON DUPLICATE KEY UPDATE column1 = value1, column2 = value2, ...;

Example: Suppose you have a users table with user_id as the primary key and you want to insert a new user or update the user's email if they already exist.

INSERT INTO users (user_id, name, email)
VALUES (1, 'John Doe', '')

Method 2: Using REPLACE INTO

Another approach is the REPLACE INTO statement. It first tries to insert the data; if a duplicate key error occurs, it deletes the existing record and then inserts the new one.


REPLACE INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Example: Using the same users table:

REPLACE INTO users (user_id, name, email)
VALUES (1, 'John Doe', '');

Best Practices and Considerations

  • Ensure that your table has a primary key or a unique index. This is crucial for accurately identifying duplicates.
  • Understand the performance implications. REPLACE INTO might be less efficient than INSERT ... ON DUPLICATE KEY UPDATE because it involves deleting and inserting instead of updating.
  • Be cautious with REPLACE INTO as it can inadvertently remove rows due to the delete-insert behavior.


Efficiently managing data in a MySQL database is key to maintaining performance and data integrity. The 'upsert' operation, using either INSERT ... ON DUPLICATE KEY UPDATE or REPLACE INTO, offers a streamlined way to handle scenarios where you need to insert or update records based on their existence. Understanding and utilizing these methods will enhance your database management skills and contribute to smoother, more efficient database operations.