Detailed description of mysql replace into usage

Detailed description of mysql replace into usage

The replace statement is generally similar to insert. However, if there is a primary or unique index in the table, if the inserted data is the same as the original primary key or unique, the original data will be deleted and a new one will be added. Therefore, sometimes executing a replace statement is equivalent to executing a delete and insert statement. Let’s go straight to the example:

Create a test table with three fields: id, title, and uid. id is the auto-increment primary key and uid is the unique index.

CREATE TABLE `test` (
 `Id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(25) DEFAULT NULL COMMENT 'Title',
 `uid` int(11) DEFAULT NULL COMMENT 'uid',
 PRIMARY KEY (`Id`),
 UNIQUE KEY `uid` (`uid`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

insert into test(title,uid) VALUES ('Hello','1');
insert into test(title,uid) VALUES ('National Day','2');

The results are as follows:

When using replace into to insert data:

REPLACE INTO test(title,uid) VALUES ('This time is 8 days off','3');

When uid exists, use replace into statement

REPLACE INTO test(title,uid) VALUES ('This is the first data of Uid=1','1');

I didn't expect that when MySQL encounters data conflicts (that is, when uid duplicates occur), it actually deletes the old record and then writes the new record. Through the above examples, I believe that bloggers can see:

replace into is similar to insert, except 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 (determined by the primary key or unique index), delete this row of data first, and then insert the new data. 2. Otherwise, insert the new data directly.

Please note that the table into which data is inserted must have a primary key or a unique index! Otherwise, replace into will directly insert the data, which will result in duplicate data in the table.

MySQL replace into has three forms:

1. replace into tbl_name(col_name, ...) values(...)

2. replace into tbl_name(col_name, ...) select ...

3. replace into tbl_name set col_name=value, ...

The first form is similar to the usage of insert into.

The second usage of replace select is also similar to insert select. This usage does not necessarily require the column names to match. In fact, MYSQL does not even care about the column names returned by the select. What it needs is the position of the column. For example, replace into tb1( name, title, mood) select rname, rtitle, rmood from tb2; This example uses replace into to import all data from tb2 into tb1.

The third replace set usage is similar to the update set usage. Using an assignment such as "SET col_name = col_name + 1", the reference to the column name on the right will be treated as DEFAULT(col_name). Therefore, this assignment is equivalent to SET col_name = DEFAULT(col_name) + 1.

The first two forms are used more frequently. The keyword "into" can be omitted, but it is better to add "into" so that the meaning is more intuitive. In addition, for columns that have no given values, MySQL will automatically assign default values ​​to these columns.

You may also be interested in:
  • Usage and difference analysis of replace into and insert into on duplicate key update in MySQL
  • Explanation of the usage of replace and replace into in MySQL
  • Detailed explanation of replace into example in mysql
  • A Brief Analysis of MySQL replace into Statement (Part 2)
  • A brief analysis of MySQL replace into statement (I)
  • Detailed explanation of the usage of replace into statement in MySQL
  • A brief analysis of the usage of MySQL replace into
  • The real difference between MySQL's Replace into and Insert into on duplicate key update
  • Analysis of the differences between REPLACE INTO and INSERT INTO in MySQL
  • Use of MySQL Replace INTO

<<:  Understanding JSON (JavaScript Object Notation) in one article

>>:  Steps to restore code from a Docker container image

Recommend

Use tomcat to deploy SpringBoot war package in centos environment

Prepare war package 1. Prepare the existing Sprin...

js to achieve simple image drag effect

This article shares the specific code of js to ac...

How to use vue-video-player to achieve live broadcast

Table of contents 1. Install vue-video-player 2. ...

Ubuntu 15.04 opens mysql remote port 3306

Ubuntu 15.04 opens MySQL remote port 3306. All th...

CSS stacking and z-index example code

Cascading and Cascading Levels HTML elements are ...

Differences between FLOW CHART and UI FLOW

Many concepts in UI design may seem similar in wo...

MySQL 8 new features: Invisible Indexes

background Indexes are a double-edged sword. Whil...

Vue.js implements simple timer function

This article example shares the specific code of ...

Solve the problem of the container showing Exited (0) after docker run

I made a Dockerfile for openresty on centos7 and ...

CSS achieves highly adaptive full screen

When writing my own demo, I want to use display:f...

Detailed steps to start the Django project with nginx+uwsgi

When we develop a web project with Django, the te...

How to display web pages properly in various resolutions and browsers

The key codes are as follows: Copy code The code i...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...