Record a pitfall of MySQL update statement update

Record a pitfall of MySQL update statement update

background

Recently, I executed a DML statement during an online operation. I thought it was foolproof, but the application reported that it was not updated and the data remained unchanged. After investigation, I found that I had written the statement incorrectly, which caused the result of the update statement to be inconsistent with my expectations.

Re-enactment

For the convenience of demonstration, create a user table and insert five records at the same time.

create table user(
id int(12) comment 'user primary key id',
name varchar(36) comment 'User name',
age int(12) comment 'age');

insert into user values ​​(1,'one',11),(2,'two',12),(3,'three',13),(4,'four',15),(5,'five',15);

After the execution is completed, the data in the user table is as follows:

+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | one | 11 |
| 2 | two | 12 |
| 3 | three | 13 |
| 4 | four | 15 |
| 5 | five | 15 |
+------+-------+------+

Now I need to change all ages to 10 and usernames to user - assuming this operation makes sense - the DML statement I submit to the operation and maintenance is as follows:

update user set age=10 and name='user';

When I refresh the user table, I see that all the data in the table after executing the update statement is as follows:

+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | one | 0 |
| 2 | two | 0 |
| 3 | three | 0 |
| 4 | four | 0 |
| 5 | five | 0 |
+------+-------+------+

A magical thing happened. The age field was all updated to 0, while the name field was not modified at all!

Causes and Corrections

The cause of the error is actually very simple, the update statement is written incorrectly. The syntax of the update statement in MySQL is

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
```sql

If updating multiple fields, adjacent fields should be separated by commas instead of `and`. If the update statement uses "and" as the separator between multiple fields, as I submitted to the operation and maintenance at the beginning, this update statement will eventually become ```sql
update user set age=(10 and name='user');

As a judgment statement with a boolean return value, (10 and name='user') will be mapped to 1 or 0. There is a 99.999% chance that the first updated variable will be updated to incorrect data.

The correct update statement should be

update user set age=10, name='user';

lesson

Before submitting DML statements, try the basic SQL syntax in a test environment to make sure you remember it correctly.

summary

The syntax of the update statement in MySQL is

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

The above is the details of a pitfall of using the MySQL update statement. For more information about MySQL update statements, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Summary of Mysql cross-table update multi-table update sql statement
  • Tutorial on viewing execution statements and update logs in real time through MySQL logs
  • SQL UPDATE update statement usage (single column and multiple columns)
  • Understanding MySQL Locking Based on Update SQL Statements
  • Java reflection JavaBean object automatically generates insert, update, delete, query sql statement operations
  • Implementation of update set from usage in sql update statement
  • Analysis of the execution process of a SQL update statement

<<:  Introduction to HTML DOM_PowerNode Java Academy

>>:  Solution to occasional crash of positioning background service on Linux

Recommend

How to export and import .sql files under Linux command

This article describes how to export and import ....

Detailed explanation of the wonderful CSS attribute MASK

This article will introduce a very interesting at...

The images in HTML are directly replaced by base64 encoded strings

Recently, I came across a webpage that had images ...

Detailed explanation of the difference between tinyint and int in MySQL

Question: What is the difference between int(1) a...

In-depth analysis of the Tomcat server of Centos 7 system

Table of contents 1. The origin of tomcat 1. Tomc...

In-depth explanation of currying of JS functions

Table of contents 1. Supplementary knowledge poin...

Summary of methods to check whether the port is open in Linux

Method 1: Use lsof command We can use the lsof co...

How to use ES6 class inheritance to achieve a gorgeous ball effect

Table of contents introduce Implementation steps ...

How to modify the ssh port number in Centos8 environment

Table of contents Preface start Preface The defau...

js to achieve floor scrolling effect

This article uses jQuery to implement the sliding...

Let's talk about the two functions of try catch in Javascript

The program is executed sequentially from top to ...

HTML code that can make IE freeze

We simply need to open any text editor, copy the f...

MySQL Basic Tutorial Part 1 MySQL5.7.18 Installation and Connection Tutorial

Starting from this article, a new series of artic...