Home  >  Article  >  Database  >  Detailed explanation of replace into in mysql

Detailed explanation of replace into in mysql

小云云
小云云Original
2018-01-17 09:57:131554browse

replace into is similar to insert in function. The difference is that replace into first tries to insert data into the table. This article mainly introduces the relevant information on the detailed explanation of mysql's replace into example. Friends who need it can refer to it. I hope it can help everyone.

Mysql's replace into example detailed explanation

1. If it is found that this row of data already exists in the table (judged based on the primary key or unique index), delete this row of data first. Then insert new data.

2. Otherwise, insert new data directly.

It should be noted that the table into which data is inserted must have a primary key or a unique index! Otherwise, replace into will insert data directly, which will result 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 corresponding to the insertion (general users get the primary key order number of the order table)


##

SELECT LAST_INSERT_ID() from dual

Related recommendations:


Analysis of the difference between REPLACE INTO and INSERT INTO in MySQL

sql replace into usage and implementation statements

REPLACE INTO and INSERT INTO in MySQL Difference analysis_MySQL

The above is the detailed content of Detailed explanation of replace into in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn