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

Detailed explanation of replace into example of mysql

黄舟
黄舟Original
2017-06-04 09:55:541424browse

This article mainly introduces the relevant information about the detailed explanation of the replace into instance of mysql. Friends in need can refer to the detailed explanation of the replace into instance of

##mysql

replace into has a similar function to insert. The difference is that replace into first tries to

insert data into the table.

1. If it is found that this row of data already exists in the table (judged based on the primary key or unique

index ), first delete this row of data, and 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 part of the corresponding insertion Primary key (general users obtain the primary key order number of the order table)

SELECT LAST_INSERT_ID() from dual

The above is the detailed content of Detailed explanation of replace into example of 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