The difference between replace into and insert into on duplicate key Usage of replace
Usage of Insert into …on duplicate key
Experimental demonstration 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; Table Data [email protected] (helei)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 1 | He Lei | 26 | | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | +----+-----------+-----+ 3 rows in 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 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 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)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 0 | | 5 | Aixuan | 0 | +----+-----------+-----+ 4 rows in set (0.00 sec) Usage of replace When there is no key conflict, replace into is equivalent to insert, and the default values of other columns are used. When a key conflict occurs, the auto-increment column is updated to replace the conflicting column, and the remaining columns are set to their default values. Insert into …on duplicate key: [email protected] (helei)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 0 | | 5 | Aixuan | 0 | +----+-----------+-----+ 4 rows in 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)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 100 | | 5 | Aixuan | 0 | +----+-----------+-----+ 4 rows in set (0.00 sec) [email protected] (helei)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 100 | | 5 | Aixuan | 0 | +----+-----------+-----+ 4 rows in 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)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 100 | | 5 | Aixuan | 120 | +----+-----------+-----+ 4 rows in set (0.00 sec) [email protected] (helei)> insert into helei1 (name) values('does not exist') on duplicate key update age=80; Query OK, 1 row affected (0.00 sec) [email protected] (helei)> select * from helei1; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 2 | Xiao Ming | 28 | | 3 | Xiaohong | 26 | | 4 | He Lei | 100 | | 5 | Aixuan | 120 | | 8 | does not exist | 0 | +----+-----------+-----+ 5 rows in set (0.00 sec) Summarize The usage of replace into is equivalent to performing a delete operation first and then an insert operation if a conflicting key is found. The default value is used for unspecified columns. This will cause the auto-increment primary key to change. If there are foreign keys in the table or the business logic depends on the primary key, an exception will occur. Therefore, it is recommended to use Insert into …on duplicate key. Due to the rush of writing time, some errors or inaccuracies are inevitable in the article. We sincerely ask readers to criticize and correct any inappropriateness. Well, the above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: How to upgrade https under Nginx
>>: Detailed explanation of the practical application of regular expressions in JavaScript
Table of contents Preface Type Inference Truth va...
<br />Table is a tag that has been used by e...
Preface In Windows, you can start multiple MySQL ...
Find the problem When we display the contents in ...
1. Background 1. The front end uses vue + vuex + ...
This article shares the specific code of Javascri...
In MySQL, the LOAD_FILE() function reads a file a...
Overlay network analysis Built-in cross-host netw...
Table of contents Preface Mysql case when syntax:...
Table of contents JVM Class Loader Tomcat class l...
In the previous article, I wrote a cross-table up...
This article describes the steps to install the p...
Let me first introduce to you that the node proce...
Why do we need permission management? 1. Computer...
1. W3C versions of flex 2009 version Flag: displa...