Non-standard implementation code for MySQL UPDATE statement

Non-standard implementation code for MySQL UPDATE statement

Today I will introduce to you a difference between the UPDATE statement in the MySQL database and the SQL standard (and other databases). If we don't pay attention to this problem, it may lead to unexpected results.

Let's start by creating a simple example table:

CREATE TABLE t1(
 id int, 
 col1 int, 
 col2 int
); 

INSERT INTO t1 VALUES (1, 1, 1);

SELECT * FROM t1;
id|col1|col2|
--|----|----|
 1| 1| 1|
————————————————
Copyright Statement: This article is an original article by CSDN blogger "Teacher Tony Who Doesn't Cut Hair", and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/horses/article/details/110238573CREATE TABLE t1(
 id int, 
 col1 int, 
 col2 int
); 

INSERT INTO t1 VALUES (1, 1, 1);

SELECT * FROM t1;
id|col1|col2|
--|----|----|
 1| 1| 1|

Then, we update the data in table t1:

UPDATE t1 
SET col1 = col1 + 1,
  col2 = col1
WHERE id = 1;

SELECT col1, col2 
FROM t1;

What are the results of the col1 and col2 fields returned by the query statement?

  • For the SQL standard and other database implementations, the results are 2 and 1 respectively.
  • But with MySQL the results are 2 and 2 respectively!

For MySQL, if an UPDATE statement uses a previously updated column (col1) in an expression (col2 = col1), the updated value of the column (2) will be used instead of the original value (1).

Note that this implementation in MySQL differs from the SQL standard. We also tested other databases, including Oracle, Microsoft SQL Server, PostgreSQL, and SQLite, whose implementations follow the SQL standard.

If we want to achieve the same effect in MySQL as in standard SQL, we can adjust the order of the updated fields in the UPDATE statement. For example:

UPDATE t1 
SET col2 = col1,
  col1 = col1 + 1
WHERE id = 1;

This way, the column col2 is updated before col1, using the old value of col1 (1), and the result is consistent with the SQL standard.

Conclusion: Generally, when writing UPDATE statements, we do not need to worry about the update order of multiple fields. However, due to MySQL implementation issues, we need to pay attention to their grammatical order.

This is the end of this article about the non-standard implementation of MySQL UPDATE statement. For more non-standard content related to MySQL UPDATE statement, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Practical MySQL + PostgreSQL batch insert update insertOrUpdate
  • Difference between MySQL update set and and
  • mysql update case update field value is not fixed operation
  • Summary of Mysql update multi-table joint update method
  • Explanation of mysql transaction select for update and data consistency processing
  • A "classic" pitfall of MySQL UPDATE statement

<<:  Summary of Several Methods for Implementing Vertical Centering with CSS

>>:  Vue implements infinite loading waterfall flow

Recommend

20 JavaScript tips to help you improve development efficiency

Table of contents 1. Declare and initialize array...

Briefly talk about mysql left join inner join

Preface I have been busy developing a cold chain ...

Example code for mixing float and margin in CSS

In my recent studies, I found some layout exercis...

How to install MySQL database on Ubuntu

Ubuntu is a free and open source desktop PC opera...

Summary of some common configurations and techniques of Nginx

Preface This article lists several common, practi...

Summary of pitfalls encountered in installing mysql and mysqlclient on centos7

1. Add MySQL Yum repository MySQL official websit...

Steps to introduce PWA into Vue project

Table of contents 1. Install dependencies 2. Conf...

The difference between where and on in MySQL and when to use them

When I was writing join table queries before, I a...

Detailed steps to build a file server in Windows Server 2012

The file server is one of the most commonly used ...

Detailed explanation of MySQL semi-synchronization

Table of contents Preface MySQL master-slave repl...

MySQL 8.0.25 installation and configuration method graphic tutorial

The latest download and installation tutorial of ...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...