How to write an SQL statement to update data in MySQL?
To modify data in MySQL, the UPDATE keyword is used, with the following syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
- table_name: the name of the table to be modified.
- Column1, column2, …: The names of the columns to be modified.
- value1, value2, etc. are the new values to be edited.
- Condition in WHERE clause is optional and is used to specify the condition for the modification. If no condition is specified, it will modify all rows in the table.
Original: 我喜歡在週末睡懶覺。
Paraphrased: I enjoy sleeping in on weekends.
Assuming there is a table called users containing columns id, name, and age, now the record with id 1 needs to be updated to have the name “John” and age of 25 with the following statement:
UPDATE users
SET name = 'John', age = 25
WHERE id = 1;
To change the value of the age column for all records in the table to 30, you can use the following statement:
UPDATE users
SET age = 30;
Caution: When using modifying statements, make sure to use the WHERE clause effectively to limit the scope of the changes and avoid accidentally modifying or deleting data.