Avoid replacing the logic of SQL MySQL pit father operations

replace into and insert into on duplicate key difference
replace usage

When the conflict is not equivalent to insert the remaining columns default value
when the key conflict, since additional update, replace conflict columns, the remaining columns defaults
Com_replace will add 1
Innodb_rows_updated will add 1

Insert into ... on duplicate key usage

Corresponds to the insert does not collide, the default value of the remaining columns
, when the key conflict, only update corresponding field values.
Com_insert will add 1
Innodb_rows_inserted will increase 1

Experiments show
table structure

create table helei1(
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL DEFAULT '',
age tinyint(3) unsigned NOT NULL default 0,
PRIMARY KEY(id),
UNIQUE KEY uk_name (name)
)
ENGINE=innodb AUTO_INCREMENT=1 
DEFAULT CHARSET=utf8;
</br>
复制代码

Table data

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 1 | He Lei | 26 |
| 2 | Xiao Ming | 28 |
| 3 | red | 26 |
+ ---- + - + ----- + ---------
3 rows in the SET (0.00 sec)

replace into usage

[email protected] (helei)> replace into helei1 (name) values('贺磊');
Query OK, 2 rows affected (0.00 sec)

[email protected] (helei)> select * from helei1;
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 2 | 小明 | 28 |
| 3 | 小红 | 26 |
| 4 | 贺磊 | 0 |
+----+-----------+-----+
3 rows in set (0.00 sec)
[email protected] (helei)> replace into helei1 (name) values('爱璇');
Query OK, 1 row affected (0.00 sec)

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 2 | Bob | 28 |
|. 3 | Alice | 26 is |
|. 4 | Lei He | 0 |
|. 5 | love Xuan | 0 |
---- + ----- + ----------- + +
4 rows in the SET (0.00 sec)

replace usage
when no key conflict, replace into the equivalent insert, the remaining columns default value
when the key conflict, since additional update, replace conflict columns, the remaining columns defaults

Insert into …on duplicate key:

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 2 | Bob | 28 |
|. 3 | Alice | 26 is |
|. 4 | Lei He | 0 |
|. 5 | love Xuan | 0 |
---- + ----- + ----------- + +
4 rows in the SET (0.00 sec)

[email protected] (helei)> insert into helei1 (name,age) values('贺磊',0) on duplicate key update age=100;
Query OK, 2 rows affected (0.00 sec)

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 2 | Bob | 28 |
|. 3 | Alice | 26 is |
|. 4 | Lei He | 100 |
|. 5 | love Xuan | 0 |
---- + ----- + ----------- + +
4 rows in the SET (0.00 sec)

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 2 | Bob | 28 |
|. 3 | Alice | 26 is |
|. 4 | Lei He | 100 |
|. 5 | love Xuan | 0 |
---- + ----- + ----------- + +
4 rows in the SET (0.00 sec)

[email protected] (helei)> insert into helei1 (name) values('爱璇') on duplicate key update age=120;
Query OK, 2 rows affected (0.01 sec)

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 2 | Bob | 28 |
|. 3 | Alice | 26 is |
|. 4 | Lei He | 100 |
|. 5 | love Xuan | 120 |
---- + ----- + ----------- + +
4 rows in the SET (0.00 sec)

[email protected] (helei)> insert into helei1 (name) values('不存在') on duplicate key update age=80;
Query OK, 1 row affected (0.00 sec)

[email protected] (helei)> the SELECT * from helei1;
+ ---- + ----- + ----------- +
| the above mentioned id | name | Age |
+ ---- + ----- + ----------- +
| 2 | Bob | 28 |
|. 3 | Alice | 26 is |
|. 4 | Lei He | 100 |
|. 5 | love Xuan | 120 |
| 8 | absent | 0 |
+ ---- + ----- + ----------- +
5 rows in the SET (0.00 sec)

Summary
replace into this usage, if a conflict is found corresponding to the key, a delete operation to do first, do an insert operation, unspecified column default value, this will lead to changes in the key generation increment, if the outer table is present primary key or keys dependent on the business logic, the exception occurs. It is recommended to use Insert into ... on duplicate key. Since the preparation time is very short, and the text will inevitably be some errors or inaccuracies, inadequacies urge readers criticism.

Reproduced in: https: //juejin.im/post/5d020df8e51d45775746b929

Guess you like

Origin blog.csdn.net/weixin_34099526/article/details/93181798