How to Use MysqlONDUPLICATEKEYUPDATE

Source: Internet
Author: User
This article describes how to use the ONDUPLICATEKEYUPDATE command in mysql. You can use this syntax to determine whether a record exists when inserting a record. If not, insert the record. Otherwise, update the record, which is convenient, you do not need to execute two SQL statements. For more information, see.

This article describes how to use the on duplicate key update command in mysql. You can use this syntax to determine whether a record exists when inserting a record. If not, insert the record. Otherwise, UPDATE the record, it is very convenient. You do not need to execute two SQL statements. If you need them, you can refer to them.

ON DUPLICATE KEY UPDATE

First look at two SQL statements

The Code is as follows:


Insert into osc_visit_stats (stat_date, type, id, view_count) VALUES (?,?,?,?) On duplicate key update view_count = view_count +?


The osc_visit_stats table has a composite primary key (stat_date, type, id)

Multi-Field update

The Code is as follows:

Insert into osc_space_visit_records (space, user, visit_count, ip, visit_time) VALUES (?,?,?,?,?) On duplicate key update visit_count = visit_count + 1, ip = ?, Visit_time = NOW ()


If you specify on duplicate key update and insert a row, DUPLICATE values will appear in a UNIQUE index or primary key, the old row will be updated. For example, if column a is defined as UNIQUE and contains a value of 1, the following two statements have the same effect:

The Code is as follows:

Mysql> insert into table (a, B, c) VALUES (1, 2, 3)

-> On duplicate key update c = c + 1;

Mysql> UPDATE table SET c = c + 1 WHERE a = 1;

If a row is inserted as a new record, the value of the affected row is 1. If the original record is updated, the value of the affected row is 2.

Note:If column B is also a unique column, INSERT is equivalent to this UPDATE statement:

The Code is as follows:
Mysql> UPDATE table SET c = c + 1 WHERE a = 1 OR B = 2 LIMIT 1;

If a = 1 OR B = 2 matches multiple rows, only one row is updated. Generally, you should avoid using the on duplicate key clause for tables with multiple unique keywords.

You can use the VALUES (col_name) function in the UPDATE clause to reference the column VALUES from the INSERT section of the INSERT... UPDATE statement. In other words, if there is no duplicate keyword conflict, the VALUES (col_name) in the UPDATE clause can reference the value of the inserted col_name. This function is particularly applicable to multiline inserts. The VALUES () function only makes sense in the INSERT... UPDATE statement. Otherwise, NULL is returned.

Example:

The Code is as follows:

Mysql> insert into table (a, B, c) VALUES (1, 2, 3), (4, 5, 6)

-> On duplicate key update c = VALUES (a) + VALUES (B );

This statement serves the same purpose as the following two statements:

The Code is as follows:

Mysql> insert into table (a, B, c) VALUES (1, 2, 3)

-> On duplicate key update c = 3;

Mysql> insert into table (a, B, c) VALUES (4, 5, 6)

-> On duplicate key update c = 9;

When you use on duplicate key update, the DELAYED option is ignored.


Example:

The Code is as follows:
Mysql> insert into table (a, B, c) VALUES (1, 2, 3), (4, 5, 6) on duplicate key update c = VALUES (a) + VALUES (B );

This statement serves the same purpose as the following two statements:

The Code is as follows:
Mysql> insert into table (a, B, c) VALUES (4, 5, 6) ON DUPLICATE KEY UPDATE c = 9;

When you use on duplicate key update, the DELAYED option is ignored.

Summary:DELAYED is used as a quick insert, but it is not very concerned about the invalidation and improves the insert performance.

Ignore only follows that the record corresponding to the primary key does not exist. If no record exists, the record is added. If yes, the record is ignored.

On duplicate key update operation when adding, pay attention to non-primary KEY columns, pay attention to the difference with ignore. If yes, update the specified column. If no, add the column.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.