mysql special syntax insert into .. on duplicate key update .. detailed explanation

I. Introduction

In daily development, we often encounter such a requirement: check whether a certain record exists, create a new record if it does not exist, and update some fields if it exists.

For example, the following pseudocode:

$row = mysql_query($result);

if($row){
    
    

mysql_execute('update ...');

}else{
    
    

mysql_execute('insert ...');

}

二、insert into … on duplicate key update …

For this purpose, MySql provides the syntax of insert into ... on duplicate key update ...:

  • When inserting, if the data of the insert will cause a conflict of the unique index (including the primary key index), that is, the unique value is repeated, the insert operation will not be performed, but the subsequent update operation will be performed.

Note: This is unique to MYSQL, not SQL standard syntax;

1. Processing logic

The insert into ... on duplicate key update ... statement is based on the unique index to determine whether the record is duplicated;

  • If there is no record, insert it, and the number of affected rows is 1;
  • If a record exists, the field can be updated, and the number of rows affected is 2;
  • If a record exists and the updated value is the same as the original value, the number of affected rows is 0.

If the table has multiple unique indexes at the same time, it will only make a duplicate judgment based on the first unique index that has a corresponding value in the database:

2. Example:

Table Structure

CREATE TABLE `user2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(94) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `gender` int(1) DEFAULT NULL,
  `type` int(1) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idex_name` (`userName`) USING BTREE,
  KEY `idx_type` (`type`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

There is a primary key id and a unique index idx_userName in the user2 table;

1> There is no record, insert it

insert into user2(userName, age, gender) VALUES("saint33", 99, 1) on DUPLICATE KEY UPDATE age = 88;

insert image description here

2> There is a record and the field can be updated

insert into user2(userName, age, gender) VALUES("saint33", 99, 1) on DUPLICATE KEY UPDATE age = 88;

insert image description here

3> There are records, but the field cannot be updated

insert into user2(userName, age, gender) VALUES("saint33", 99, 1) on DUPLICATE KEY UPDATE age = 88;

insert image description here

4> When there are multiple unique indexes

If the table has multiple unique indexes at the same time, it will only make a duplicate judgment based on the first unique index that has a corresponding value in the database:

1) The record with id = 12 does not exist in the database, but the record with userName="saint22" exists, so the duplicate judgment will be made based on the second unique index userName;

insert into user2(id, userName, age, gender) VALUES(12, "saint22", 99, 1) on DUPLICATE KEY UPDATE age = VALUES(gender) + 80;

insert image description here

2) The record with id = 10 exists in the database, and the record with userName="saint22" exists, so the duplicate judgment will be made based on the first unique index id;

insert into user2(id, userName, age, gender) VALUES(10, "saint22", 99, 1) on DUPLICATE KEY UPDATE age = VALUES(gender) + 90;

insert image description here

3. The Update clause gets the value of the inset part

The Update clause can use values(col_name) to get the value of the insert part:

insert into user2(userName, age, gender) VALUES("saint22", 99, 1) on DUPLICATE KEY UPDATE age = VALUES(age) + 100;

insert image description here

Note: The VALUES() function is only meaningful in the INSERT...UPDATE statement, and returns NULL at other times;

4、last_insert_id()

If the table contains an auto_increment field, after using insert ... on duplicate key update to insert or update, last_insert_id() returns the value of the auto_increment field.

Guess you like

Origin blog.csdn.net/Saintmm/article/details/127444881