Home > Database > Mysql Tutorial > How to use MySQL update command

How to use MySQL update command

王林
Release: 2023-05-29 17:07:49
forward
1921 people have browsed it

1. Update syntax

The syntax format of the update command is as follows:

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

The points that need explanation are as follows:

  • table_name: Specify the name of the table that needs to be modified.

  • column1=value1,column2=value2,...: Multiple assignment statements are separated by "," to indicate the columns that need to be modified and the modified values.

  • The conditional statement "WHERE some_column=some_value" is used to filter records that need to be modified. Only records that meet this condition will be modified. The column name that needs to be filtered is some_column, and the value that needs to be filtered is some_value.

Omitting conditional statements in the update command will cause all records in the table to be modified. You need to be very careful when using the update command and handle it with care.

2. Update Example

The following uses an example to demonstrate the actual use of the update command. Suppose there is a table named student, which includes several fields such as id, name, age, gender, class, etc. The information of student No. 001 needs to be modified to change his age to 20.

First, you need to log in to the MySQL database,

mysql -u root -p密码
Copy after login

Then, select the database that needs to be modified, for example, select the database named test:

use test;
Copy after login

Before executing the update command, please Make sure you give enough thought to what you're going to do. In this example, the execution instruction is as follows:

UPDATE student SET age = 20 WHERE id = '001';
Copy after login

The above instruction means to modify the age of the student with middle school number 001 in the student table to 20 years old. If the modification is successful, you will receive the following prompt:

Query OK, 1 row affected (0.01 sec)
Rows matched: 1 changed: 1 warnings: 0
Copy after login

Among them, "Query OK" means that the command was successfully executed, and 1 row affected means that the command affected 1 row, that is, the student information numbered 001 was successfully modified.

The above is the detailed content of How to use MySQL update command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template