Detailed Explanation of replace into Example of mysql

  • 2021-08-17 01:17:39
  • OfStack

Detailed Explanation of replace into Example of mysql

replace into is similar in functionality to insert except that replace into first attempts to insert data into a table.

1. If you find that there is already this row of data in the table (judged according to the primary key or the only 1 index), delete this row of data first, and then insert new data.

2. Otherwise, insert new data directly.

Note that the table inserting data must have a primary key or a only 1 index! Otherwise, replace into inserts data directly, resulting in duplicate data in the table.

There are three ways to write replace into in MySQL:

The code is as follows:


 replace into table(col, ...) values(...)
 replace into table(col, ...) select ...
 replace into table set col=value, ...

Extension: mysql gets the last primary key of the corresponding insertion (1 user gets the primary key order number of the order table)


SELECT LAST_INSERT_ID() from dual 

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: