Mysql replace into 和 update or insert

When using mysql for batch data synchronization, a situation often encountered is: some of the data obtained in batches needs to be added, and some needs to be modified. mysql provides two solutions for this 

  • REPLACE INTO Alternate INSERT INTO
  • INSERT ...  ON DUPLICATE KEY UPDATE ...

Both of these schemes require a primary key or a unique index. When there is a conflict between the primary key or the unique index, the data will be modified, otherwise the data will be inserted. Let's look at the two options in detail.

First we create tables and indexes:

show create table person; -- view table creation statement

Note: Here we have both the primary key ID and the unique index---id_card in our table, and the primary key is self-incrementing. As for why it is designed this way, it is mainly to compare the differences between the two schemes.

CREATE TABLE `person` (
  `id` int NOT NULL AUTO_INCREMENT,
  `id_card` bigint DEFAULT NULL,
  `name` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  `age` int DEFAULT NULL,
  `high` double DEFAULT NULL,
  `place` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `family` json DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_card` (`id_card`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

Table Structure: 

index: 

data: 

1、REPLACE INTO

We first modify the next two data, and insert a new data:

REPLACE INTO `zsm`.`person`(`id`, `id_card`, `name`, `age`, `high`, `place`, `family`) 
VALUES  (2, 2, '王三', 10, 172.1, '山东', NULL),
		(3, 3, '王四', 22, 173.1, 'asd', NULL),
		(4, 4, '王五', 22, 173.1, 'asd', NULL);

Results of the: 

 Data in the table: 

 From the data in the table, we can see that the data with id 2 and 3 has been modified, and a new piece of data has been inserted, which affects a total of 3 pieces of data, but the information output by sql execution says that 5 pieces of data are affected. This is because the operation process of replace into is: 

  1. If no uniqueness conflict is found, insert the data directly
  2. If a uniqueness conflict is found, delete the original data first, and then insert new data.

It is also very simple to verify this point. We execute the above SQL statement again, but remove the ID column of the primary key, because the ID is self-incrementing. If mysql deletes the original data and inserts new data, the id will increase in between.

REPLACE INTO `zsm`.`person`(`id_card`, `name`, `age`, `high`, `place`, `family`) 
VALUES  (2, '王三', 10, 172.1, '山东', NULL),
		(3, '王四', 22, 173.1, 'asd', NULL),
		(4, '王五', 22, 173.1, 'asd', NULL);

Results of the: 

Data in the table: 

Here, because I have inserted other data before, the id of the data is incremented on the previous sequence.

2、insert or update 

The syntax format of insert or update: 

INSERT INTO tableName ....
VALUES ....
ON DUPLICATE KEY UPDATE 
column1 = VALUES(column1),  // 新值替换旧值
column2 = column2 + 1,      // 值运算
.... 

Execute the SQL statement: 

INSERT INTO `zsm`.`person`(`id_card`, `name`, `age`, `high`, `place`, `family`) 
VALUES (2, '张三', 10, 172.1, '山东', NULL)
ON DUPLICATE KEY UPDATE 
    name = VALUES(name), 
    age = age + 3;

Results of the: 

 Data in the table: 

It can be seen that the affected data in the table is only the record with id_car=2, and the id has not been incremented. However, the results still show that two rows of records are affected, which is not clear yet. If there are friends who know, I hope to advise.

 At this time, if we modify the SQL statement, just remove the operation of age = age + 3 to see the execution result:

INSERT INTO `zsm`.`person`(`id_card`, `name`, `age`, `high`, `place`, `family`) 
VALUES (2, '张三', 10, 172.1, '山东', NULL)
ON DUPLICATE KEY UPDATE 
    name = VALUES(name);

Results of the: 

 No data is modified. It seems that mysql compares the columns to be modified, and only when the modified column changes, the modification operation will be performed.

Summarize: 

replace into : When there is a uniqueness conflict, delete the original data and replace it with new data; when there is no uniqueness conflict, insert the data directly

insert or update: You can set the columns that can be changed when there is a conflict. If there is a unique conflict and the value of the column to be changed changes, the modification operation will be performed; if there is no unique conflict, insert the data directly.

Guess you like

Origin blog.csdn.net/zhoushimiao1990/article/details/120728702