Tips for MySQL Databases

Source: Internet
Author: User

The following articles mainly introduce the tips of MySQL databases. First, we will explain some tips of MySQL Databases starting with the INSERT syntax. I saw the related information on the relevant websites two days ago, I think it's good. I just want to share it with you.

INSERT syntax

 
 
  1. INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]  
  2. [INTO] tbl_name [(col_name,...)]  
  3. VALUES ({expr | DEFAULT},...),(...),...  
  4. [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] 

Or:

 
 
  1. INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]  
  2. [INTO] tbl_name  
  3. SET col_name={expr | DEFAULT}, ...  
  4. [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] 

Or:

 
 
  1. INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]  
  2. [INTO] tbl_name [(col_name,...)]  
  3. SELECT ...  
  4. [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] 

1. Use of DELAYED

Use delayed insert operations

The DELAYED modifier is applied to INSERT and REPLACE statements. When the DELAYED insert operation arrives,

The server puts data rows in a queue and immediately returns a status message to the client.

You can continue the operation before the data table is actually inserted into the record. If the reader

When reading data from a table, the data in the queue will be kept until there is no reader. Then the server

Start to insert delayed data rows delayed-row) data rows in the queue. During the insert operation, the server

Check whether new read requests arrive and wait. If yes, the delayed data row queue will be suspended,

Allow the reader to continue the operation. When no reader is available, the server inserts delayed data rows again.

This process continues until the queue is empty.

Notes:

Insert delayed should be used only for the INSERT statement that specifies the 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 returns 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 of the MySQL database to the standard SQL. If duplicate keywords exist in the new table,

Or, if a warning is reported after the STRICT mode is started, IGNORE is used to control the running of the 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 (...)

In this way, you do not need to check whether there is any. If yes, ignore it. If no, add it.

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:

 
 
  1. MySQL> INSERT INTO table (a,b,c) VALUES (1,2,3)  
  2. -> ON DUPLICATE KEY UPDATE cc=c+1;  
  3. MySQL> UPDATE table SET cc=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:

 
 
  1. MySQL> UPDATE table SET cc=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:

 
 
  1. MySQL> INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)  
  2. -> ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); 

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

 
 
  1. MySQL> INSERT INTO table (a,b,c) VALUES (1,2,3)  
  2. -> ON DUPLICATE KEY UPDATE c=3;  
  3. MySQL> INSERT INTO table (a,b,c) VALUES (4,5,6)  
  4. -> ON DUPLICATE KEY UPDATE c=9; 

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

The above content is an introduction to the MySQL database. I hope you will get some benefits.

Related Article

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.