MySQL Insert syntax considerations (on DUPLICATE KEY UPDATE)

Source: Internet
Author: User
Tags mysql insert

MySQL Insert syntax considerations (on DUPLICATE KEY UPDATE)

INSERT Grammar

INSERT [Low_priority | DELAYED | High_priority] [IGNORE]

[Into] tbl_name [(Col_name,...)]

VALUES ({expr | DEFAULT},...), (...),...

[on DUPLICATE KEY UPDATE col_name=expr, ...]

Or:

INSERT [Low_priority | DELAYED | High_priority] [IGNORE]

[Into] Tbl_name

SET col_name={expr | DEFAULT}, ...

[on DUPLICATE KEY UPDATE col_name=expr, ...]

Or:

INSERT [Low_priority | High_priority] [IGNORE]

[Into] tbl_name [(Col_name,...)]

SELECT ...

[on DUPLICATE KEY UPDATE col_name=expr, ...]

First,DELAYED
Using deferred insert operations
DELAYED modifiers apply to INSERT and REPLACE statements. When the DELAYED insert operation arrives,  the server puts the data row in a queue,
And immediately returns a status message to the client so that the client can continue operations before the data table is actually inserted into the record.
If the reader reads data from the data table, the data in the queue is persisted until there is no reader. Then the server starts inserting the delay
The data row in the data row (delayed-row) queue. At the same time as the Insert operation, the server also checks to see if a new read request arrives and waits.
If there is, the deferred data line queue is suspended, allowing the reader to continue the operation. When there is no reader, the server begins inserting the deferred data row again. This process continues until the queue is empty.
Precautions:
· Insert DELAYED should be used only for insert statements that specify a value list. Server ignored for INSERT DELAYED ... the DELAYEDof the SELECT statement.
Server ignored for INSERT DELAYED ... the DELAYEDof the on DUPLICATE UPDATE statement.
Because the statement returns immediately before the row is inserted, you cannot use last_insert_id () to get the auto_increment value. The auto_increment value may be generated by the statement.
For SELECT statements, theDELAYED rows are not visible until the rows are actually inserted.
· DELAYED is ignored in the subordinate replication server because DELAYED does not produce data that is not the same as the primary server in the secondary server.
Note that the rows currently in the queue are only saved in memory until they are inserted into the table. This means that if you forcibly abort the mysqld(for example, using kill-9)
Or if mysqld stops unexpectedly, all rows that are not written to the disk will be lost.
Second,IGNORE
IGNORE is an extension of MySQL relative to standard SQL . If there are duplicate keywords in the new table,
Or, if a warning occurs after STRICT mode is started, use IGNORE to control the operation of ALTER TABLE .
If IGNOREis not specified, the copy operation is discarded when a duplicate keyword error occurs, returning to the previous step.
If IGNOREis specified, for rows with duplicate keywords, only the first row is used, and the other conflicting rows are deleted.
Also, correct the error value so that it is as close to the correct value as possible.
Insert ignore into TB (...) value (...)
This does not need to verify the existence of, there is ignored, no add

Third, onDUPLICATE KEY UPDATE

If you specify an on DUPLICATE KEY updateand the insert row causes duplicate values to occur in a UNIQUE index or PRIMARY KEY , the old row UPDATE is performed . For example, if column a is defined as UNIQUEand contains a value of 1, the following two statements have the same effect:

INSERT into table (a,b,c) VALUES On   DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;

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

NOTE: If column b is also the only column, the INSERT is equivalent to this UPDATE statement:

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. In general, you should try to avoid using the on DUPLICATE key clause on a table with multiple unique keywords.

You can use the VALUES (col_name) function from the INSERT ... in the UPDATE clause. the INSERT portion of the UPDATE statement refers to the column value. In other words, if a duplicate keyword conflict does not occur, values (col_name) in the UPDATE clause can refer to the value of the col_name being inserted. This function is especially useful for multi-row insertions. The VALUES () function is only in the INSERT ... The UPDATE statement makes sense, and returns NULLat other times.

Example:

INSERT into table (a,b,c) VALUES (4,5,6) On  DUPLICATE KEY UPDATE c=values (a) +values (b);

This statement works the same as the following two statements:

INSERT into table (a,b,c) VALUES (4,5,6) on DUPLICATE KEY UPDATE c=9;

When you use the on DUPLICATE KEY UPDATE , theDELAYED option is ignored.


Summary
1. DELAYED as a quick insert, not very concerned about the failure, improve the insertion performance.

2. Ignore only focus on the primary key corresponding record is not present, none is added, there is ignored.

3. On DUPLICATE key update operation on add, focus on non-primary key columns, note the difference from ignore , there is the update of the specified column, none added.

MySQL Insert syntax considerations (on DUPLICATE KEY UPDATE)

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.