MySQL REPLACE INTO

Profile picture for user arilio666

The MySQL replace works in the same way as the insert command, whereas here, when the old row matches the new record in the table based on the primary and unique key, This command deletes the old row before the new record which is provided in the command is inserted into it.

This statement is pretty applicable when we want to update a record in a table rather than an insert query. It provides the duplicate row into the table and will also throw a primary and unique key error. If no matching values are found in the table, the insert statement is performed as usual.

MySQL Replace Into Syntax

REPLACE INTO table_name(ColumnNames)  
VALUES(value_list);  

MySQL Replace Into Example

Today we are going to use the info table.

MySQL Replace Into Example

In this info table, let us replace the city of lokesh with Chennai.

replace into info(name, city) values ('Lokesh','Chennai')

MySQL Replace Into Example

We can see based on Lokesh here the city has been changed from the old record of Delhi to Chennai. So this is how we can use the replace into statement effectively.

Tags