ON DUPLICATE KEY UPDATE作用
First declare that ON DUPLICATE KEY UPDATE is Mysql-specific syntax, which is
the function of a pit statement. When inserting an existing record, execute Update
usage
What's the meaning? For example:
There is a piece of data in the user_admin_t table as follows
The primary key in the table is id. Now we want to insert a piece of data. The id is '1' and the password is 'the first inserted password'. The normal writing is:
INSERT INTO user_admin_t (_id,password)
VALUES ('1','第一次插入的密码')
Refresh the table data after execution, let's look at the contents of the table
At this time, a record with the primary key '_id' as '1' and 'password' as the 'password inserted for the first time' is added to the data in the table. What happens when we execute the insert statement again?
-- 执行
INSERT INTO user_admin_t (_id,password)
VALUES ('1','第一次插入的密码')
[SQL]INSERT INTO user_admin_t (_id,password)
VALUES ('1','第一次插入的密码')
[Err] 1062 - Duplicate entry '1' for key 'PRIMARY'
Mysql tells us that our primary key is conflicted. Seeing here, can we change our thinking? When inserting a record with an existing primary key, change the insertion operation to modification:
-- 在原sql后面增加 ON DUPLICATE KEY UPDATE
INSERT INTO user_admin_t (_id,password)
VALUES ('1','第一次插入的密码')
ON DUPLICATE KEY UPDATE
_id = 'UpId',
password = 'upPassword';
We execute again:
[SQL]INSERT INTO user_admin_t (_id,password)
VALUES ('1','第一次插入的密码')
ON DUPLICATE KEY UPDATE
_id = 'UpId',
password = 'upPassword';
受影响的行: 2
时间: 0.131s
You can see the affected behavior 2, this is because the original record is modified instead of inserting, look at the data in the table:
The original 'id' of '1' has been changed to 'UpId', and 'password' has also been changed to 'upPassword', which solves the problem of repeated insertion.
expand
When inserting multiple pieces of data, not only the existing data in the table, but also the data that needs to be newly inserted, how will Mysql execute it? Will it report an error?
In fact, Mysql is far more powerful than we imagined. It will intelligently choose to update or insert. Let's try:
INSERT INTO user_admin_t (_id,password)
VALUES
('1','第一次插入的密码') ,
('2','第二条记录')
ON DUPLICATE KEY UPDATE
_id = 'UpId',
password = 'upPassword';
run sql
[SQL]INSERT INTO user_admin_t (_id,password)
VALUES
('1','第一次插入的密码') ,
('2','第二条记录')
ON DUPLICATE KEY UPDATE
_id = 'UpId',
password = 'upPassword';
受影响的行: 3
时间: 0.045s
Mysql performs one modification and one insert, and the data in the table is:
VALUESEdit
Then the problem comes again. Some people will say that the ON DUPLICATE KEY UPDATE is followed by a fixed value. What if I want to insert different values into different records?
INSERT INTO user_admin_t (_id,password)
VALUES
('1','多条插入1') ,
('UpId','多条插入2')
ON DUPLICATE KEY UPDATE
password = VALUES(password);
One of the methods can change the following modification conditions to VALUES(password), and dynamically pass in the value to be modified, and execute the following:
[SQL]INSERT INTO user_admin_t (_id,password)
VALUES
('1','多条插入1') ,
('UpId','多条插入2')
ON DUPLICATE KEY UPDATE
password = VALUES(password);
受影响的行: 4
时间: 0.187s
Successfully modified two records, refresh the table
We successfully changed passwords with different ids to different values
Summarize
In fact, there are many ways to modify, including SET or REPLACE, and even the transaction can be saved. ON DUPLICATE KEY UPDATE allows us to easily complete the development requirements of repeated insertion, but it is a unique grammar of Mysql, and you should pay more attention when using it. Whether the primary key and the inserted value are the key and Value we want to insert or modify.