Repeated insertion of ON DUPLICATE KEY, affecting the number of rows 2rows

when using insert on duplicate key update to insert duplicate data, only the data of the duplicate unique index is updated, and the number of affected rows is returned as 2 rows.
looking up data means that the operation will perform two operations:
1.insert table attempts to insert [try to insert, there is duplicate data, and there should be no table data change at this time]
2.update updates the value of the unique index [change rows 1]
, so I"m confused about where the other change occurs. Ask a question for the first time, and ask the boss to solve the puzzle

.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
table structure

CREATE TABLE test1
(
  id     INT AUTO_INCREMENT
    PRIMARY KEY,
  name   VARCHAR(20) NOT NULL,
  stu_id INT         NOT NULL
)
  ENGINE = InnoDB;

id unique

insert into test1 VALUES ("4","","2") on DUPLICATE KEY UPDATE stu_id=stu_id+1;
commit;
Jan.16,2022

insert on duplicate key update will not delete data, so the replace statement will
affect the reason why the number of rows is 2. You can refer to this article mysql self-increasing id query


insert on duplicate key update possible execution process:

  1. insert data first
  2. < del > duplicate data detected, Delete data < / del >
  3. use update operation instead

the reason may appear in the second step

official website documentation:

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to the mysql_real_connect () C API function when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.

)
insert-on-duplicate
Menu