Mysqlinsert syntax _ MySQL

Source: Internet
Author: User
Mysqlinsert syntax INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES ({expr | DEFAULT },...), (...),... [on duplicate key UPDATEcol_name = expr,...] or: INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name SETcol_name = {expr | DEFAULT },... [on duplicate key UPDATEcol_name = expr,...] or: INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] SELECT... [on duplicate key UPDATEcol_name = expr,...] 1. Use of DELAYED

Use the DELAYED modifier of the delayed insert operation to apply to INSERT and REPLACE statements. When the DELAYED insert operation arrives, the server puts the data row into a queue and immediately returns a status message to the client, in this way, the client can continue the operation before the data table is actually inserted into the record. If the reader reads data from the data table, the data in the queue will be kept until there is no reader. Then, the server inserts data rows in the delayed-row queue. During the insert operation, the server also checks whether new read requests arrive and wait. If yes, the delayed data row queue is suspended and the reader is allowed to continue the operation. When no reader is available, the server inserts delayed data rows again. This process continues until the queue is empty. Note: · insert delayed should be used only for the INSERT statement of the specified value list. The server ignores the DELAYED used for the insert delayed... SELECT statement. · The server ignores the DELAYED used for the insert delayed... on duplicate update statement. · Because the statement is returned immediately before the row is inserted, you cannot use LAST_INSERT_ID () to obtain the AUTO_INCREMENT value. The AUTO_INCREMENT value may be generated by the statement. · For SELECT statements, DELAYED rows are invisible until these rows are indeed inserted. · DELAYED is ignored in the slave replication server, because DELAYED does not generate data different from the master server in the slave server. Note: Currently, each row in the queue is only stored in the memory until they are inserted into the table. This means that if you forcibly stop mysqld (for example, use kill-9) or if mysqld stops unexpectedly, all rows that are not written to the disk will be lost.

II. Use of IGNORE

IGNORE is an extension between MySQL and standard SQL. If there are duplicate keywords in the new TABLE, or a warning occurs after STRICT mode is started, IGNORE is used to control the running of alter table. If no IGNORE is specified, the copy operation is abandoned when a duplicate keyword error occurs, and the previous step is returned. If IGNORE is specified, only the first row is used for rows with duplicate keywords, and other conflicting rows are deleted. In addition, correct the error value so that it is as close as possible to the correct value. Insert ignore into tb (...) value (...

III. use of ON DUPLICATE KEY UPDATE

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: mysql> insert into table (a, B, c) VALUES, 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: 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: mysql> insert into table (a, B, c) VALUES (1, 2, 3), (4, 5, 6)-> ON DUPLICATE KEY UPDATE c = VALUES () + VALUES (B); this statement has the same role as the following two statements: mysql> insert into table (a, B, c) VALUES (, 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. Conclusion: DELAYED is not very concerned about the invalidation and improves the insertion 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.