When we insert data, we may encounter the problem of inserting duplicate data, but these data are not allowed to have duplicate values: CREATE TABLE stuInfo ( id INT NOT NULL COMMENT 'Serial number', name VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'Name', age INT NOT NULL DEFAULT 0 COMMENT 'Age', PRIMARY KEY (id), UNIQUE KEY uniq_name(name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='student table'; mysql> INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20),(1,'xiaosi',25),(2,'aa',24); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' Solution: 1. IGNOREUse ignore to automatically ignore duplicate records when the inserted value encounters a duplicate primary key (PRIMARY KEY) or unique key (UNIQUE KEY), without affecting the insertion of subsequent records. INSERT IGNORE INTO stuInfo (name,birthday,is_deleted) VALUES ('yoona','1990-01-05',0),('aa','1990-01-16',0),('bb','1990-01-17',0); Running results: mysql> INSERT IGNORE INTO stuInfo (id,name,age) VALUES (1,'yoona',20),(1,'xiaosi',25),(2,'aa',24); Query OK, 2 rows affected (0.02 sec) Records: 3 Duplicates: 1 Warnings: 0 mysql> select * from stuInfo; +----+-------+-----+ | id | name | age | +----+-------+-----+ | 1 | yoona | 20 | | 2 | aa | 24 | +----+-------+-----+ 2 rows in set (0.00 sec) We can see from the running results that only two rows are affected, which means that (1,'yoona',20) data is inserted, (1,'xiaosi',25) duplicate data is automatically ignored, and (2,'aa',24) non-duplicate data continues to be inserted and will not be affected by the duplicate data; 2. REPLACEWhen using replace to insert records with duplicate primary keys or unique keys, delete the duplicate records in the table before inserting them. mysql> REPLACE INTO stuInfo (name,birthday,is_deleted) VALUES ('yoona','1990-01-15',0),('yoona','1990-02-16',0),('aa','1990-01-13',0); Query OK, 4 rows affected (0.02 sec) Records: 3 Duplicates: 1 Warnings: 0 Running results: mysql> select * from stuInfo; +----+-------+------------+------------+ | id | name | birthday | is_deleted | +----+-------+------------+------------+ | 21 | yoona | 1990-02-16 | 0 | | 22 | aa | 1990-01-13 | 0 | +----+-------+------------+------------+ 2 rows in set (0.00 sec) From the output information, we can see that 4 rows are affected, which means that ('yoona','1990-01-15',0) was inserted first and then ('yoona','1990-01-15',0) was deleted. 3. ON DUPLICATE KEY UPDATEWhen the inserted record encounters a duplicate primary key or unique key, the UPDATE operation defined later will be executed. It is equivalent to performing an Insert operation first, and then performing an update operation based on the primary key or unique key. DROP TABLE IF EXISTS stuInfo; CREATE TABLE stuInfo ( id INT NOT NULL COMMENT 'Serial number', name VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'Name', age INT NOT NULL DEFAULT 0 COMMENT 'Age', PRIMARY KEY (id), UNIQUE KEY uniq_name(name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='student table'; VALUES explanation after ON DUPLICATE KEY UPDATE: VAULES(age) refers to the value of the record to be inserted Age refers to the table's own value, the inserted value. (1) First Scenario: #VALUES(age) value to be inserted 25 INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20),(1,'xiaosi',25) ON DUPLICATE KEY UPDATE age = VALUES(age) + 1; is equivalent to: INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20); UPDATE stuInfo SET age = VALUES(age) + 1 WHERE id = 1; Running results: mysql> INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20),(1,'xiaosi',25) ON DUPLICATE KEY UPDATE age = VALUES(age) + 1; Query OK, 3 rows affected (0.01 sec) Records: 2 Duplicates: 1 Warnings: 0 mysql> select * from stuInfo; +----+-------+-----+ | id | name | age | +----+-------+-----+ | 1 | yoona | 26 | +----+-------+-----+ 1 row in set (0.00 sec) (2) The second situation: #age value 20 has been inserted INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20),(1,'xiaosi',25) ON DUPLICATE KEY UPDATE age = age + 1; is equivalent to: INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20); UPDATE stuInfo SET age = age + 1 WHERE id = 1; Running results: mysql> INSERT INTO stuInfo (id,name,age) VALUES (1,'yoona',20),(1,'xiaosi',25) ON DUPLICATE KEY UPDATE age = age + 1; Query OK, 3 rows affected (0.02 sec) Records: 2 Duplicates: 1 Warnings: 0 mysql> select * from stuInfo; +----+-------+-----+ | id | name | age | +----+-------+-----+ | 1 | yoona | 21 | +----+-------+-----+ 1 row in set (0.00 sec) If you encounter duplicate inserted data, ON DUPLICATE KEY UPDATE is used to modify the inserted data. You can use it to get duplicate inserted data (directly use the field name) or get duplicate data to be inserted (values (field name)). We will not insert duplicate data. Repeat the inserted data: (1,'yoona',20) in the above example Repeat the data to be inserted: (1,'yoona',25) in the above example This is the end of this article on the detailed explanation of the solution to the duplicate insertion of MySQL primary key and unique key. For more relevant content on duplicate insertion of MySQL primary key and unique key, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Summary of knowledge points on using calculated properties in Vue
>>: Detailed explanation of the difference between a href=# and a href=javascript:void(0)
MySQL 5.7 and above versions provide direct query...
Many times we want the server to run a script reg...
Table of contents Overview Canvas API: Drawing Gr...
Table of contents Overview 1. Menu and routing pr...
Preface The database deadlocks I encountered befo...
I don’t know if you have noticed that when we ope...
Table of contents Preface 1. Uninstall MySQL 2. I...
Table of contents 1. Preface 2. Find two pop-up c...
Preface When we were writing the web page style a...
MySQL View the maximum number of connections and ...
Transactional Characteristics 1. Atomicity: After...
Common points: The DIV tag and SPAN tag treat som...
1. Download MySQL Workbench Workbench is a graphi...
This blog post is about a difficulty encountered ...
Table of contents forEach() Method How to jump ou...