MySQL UPDATE statement differs from standard SQL

Source: Internet
Author: User
Tags abs mysql update

"There is a concept called simultaneous execution in the SQL standard ."

Simultaneous execution refers to the timing of the execution of the various parts of the same clause as distinct, such as the following SQL statement

Select ABS(-1),ABS(2); +---------+--------+| ABS(-1)| ABS(2)|+---------+--------+|       1 |      2 |+---------+--------+1Rowinch Set(0.01Sec

According to SQL Standard abs ( -1) and abs (2) These two functions are " simultaneous execution "!

"mysql update with SQL standard back "

  1): in order to illustrate the problem I define the following table structure, table t contains two data columns ' x ', ' Y '

Create Table int  not NULL Primary Key int int);                   

  

  2): Add a row of data to table T

Insert  intoT (x, y)Values(1,1);Select *  fromT;+----+------+------+|Id|X|Y|+----+------+------+|  1 |    1 |    1 |+----+------+------+1Rowinch Set(0.00Sec

  

  3): execute an UPDATE statement

UpdateTSetX=X+1Y=x;Select *  fromT; +----+------+------+|Id|X|Y|+----+------+------+|  1 |    2 |    2 |+----+------+------+1Rowinch Set(0.00Sec

From the above results, it can be seen that the UPDATE statement is not executed concurrently, if the execution of the theory y=x this statement execution, the value of x will be the initial value "1" instead of the value of the self-increment "2";

If you feel that the result is not a problem for you, let's look at the next UPDATE statement, I just swap the two parts of "x=x+1" and "y=x".

  4): Execute the adjusted SQL statement

UpdateTSetY=X, X=X+1; MySQL> Select *  fromT;+----+------+------+|Id|X|Y|+----+------+------+|  1 |    3 |    2 |+----+------+------+1Rowinch Set(0.00Sec

You can see that the update in the MySQL database is not executed at the same time, it is in order, and this sequence directly affects the results of your SQL execution.

" Summary "

When you write a program, realize that MySQL is dealing with the specifics of the update statement, and that the assignment statement that precedes it will have an effect on the statements in the background;

That's because MySQL explicitly mentions this in its official documentation, in the age of the bug, and we can only say that this is a workaround to the SQL standard.

Official Document: Https://dev.mysql.com/doc/refman/8.0/en/ansi-diff-update.html

-----------------------------http://www.sqlpy.com-------------------------------------------------

-----------------------------http://www.sqlpy.com-------------------------------------------------

MySQL UPDATE statement differs from standard SQL

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.