← Back to MySQL Course | Chapter 7: Updating & Deleting | Lesson 4 of 4

REPLACE INTO

REPLACE INTO is like handing in a new form for a student who already has one: the old form is thrown out and the new one takes its place.
Syntax
sql
REPLACE INTO table_name (column1, column2)
VALUES (value1, value2);

Introduction to REPLACE INTO

REPLACE INTO works like INSERT, but with a twist for handling duplicates. If a row with the same primary key or unique index already exists, MySQL deletes the old row first and inserts the new one. If the row does not exist, it simply inserts it like a normal INSERT would.

Example: Introduction to REPLACE INTO

sql
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO users VALUES (1, 'Amit');
REPLACE INTO users VALUES (1, 'Amit Kumar');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

REPLACE INTO with Set Syntax

You can also use the SET syntax with the REPLACE statement instead of the column-list form. This is highly readable and works similarly to the UPDATE syntax. It makes it clear which columns are getting new values at a glance.

Example: REPLACE INTO with Set Syntax

sql
REPLACE INTO users SET id = 1, name = 'Amit Kumar';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

REPLACE INTO with Select Queries

You can copy data from one table to another using REPLACE INTO with a SELECT query, useful for syncing a staging table into a live one. If duplicate keys are found in the destination table, they will be updated with the new rows automatically rather than causing an error.

Example: REPLACE INTO with Select Queries

sql
REPLACE INTO live_users SELECT * FROM staging_users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

The Behind-the-Scenes Delete

When REPLACE finds a duplicate, it deletes the old row and inserts a new one behind the scenes. This means any auto-increment primary key might change if it is not explicitly provided.

It also triggers any DELETE and INSERT triggers on the table, which can surprise you if you only expected an update.

Example: The Behind-the-Scenes Delete

sql
-- REPLACE deletes the old row and inserts a new one; a fresh AUTO_INCREMENT value may be assigned
REPLACE INTO users (id, name) VALUES (1, 'Amit Kumar');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

REPLACE vs INSERT ... ON DUPLICATE KEY UPDATE

REPLACE deletes the old row and inserts a new one, resetting every column to the values you provide. INSERT ... ON DUPLICATE KEY UPDATE updates only the specified columns of the existing row instead of deleting it.

Use REPLACE when you want to overwrite all columns easily, and the UPDATE form when you want to preserve untouched columns.

Example: REPLACE vs INSERT ... ON DUPLICATE KEY UPDATE

sql
REPLACE INTO users (id, name, email) VALUES (1, 'Amit', '[email protected]'); -- resets every column
INSERT INTO users (id, name, email) VALUES (1, 'Amit', '[email protected]')
ON DUPLICATE KEY UPDATE name = 'Amit'; -- updates only name

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using REPLACE INTO when you want to update a few columns, since it deletes the old row and inserts a new one, so unspecified columns go back to defaults.
  2. Expecting it to work without a PRIMARY KEY or UNIQUE index, so it just inserts new rows.
  3. Being surprised that AUTO_INCREMENT values and foreign-key cascades are affected because the row is really deleted first.
Chapter Summary
  • UPDATE changes existing rows, and it can set multiple columns at once.
  • DELETE removes rows from a table, and a WHERE clause controls which ones.
  • REPLACE INTO inserts a row or replaces an existing one with the same key.
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.