How to update and delete data in MySQL using SQL statements?
In the MySQL database, updating and deleting data are very common operations. By using SQL statements, we can easily update and delete data in the table. This article will introduce how to use SQL statements to update and delete data in MySQL, and provide specific code examples.
Update data
In MySQL, we use the UPDATE statement to update data in the table. The basic syntax of the update statement is as follows:
UPDATE 表名 SET 列名1=新值1, 列名2=新值2, ... WHERE 条件;
Among them, table name
is the name of the table to update the data, column name 1=new value 1, column name 2=new value 2, ...
indicates the column to be updated and the corresponding new value. The WHERE
clause is optional and is used to specify the conditions for updating data.
The following is a specific example:
Suppose we have a table named students
, containing the following fields: id
, name
, age
, grade
. Now we want to update student Zhang San’s age to 18 years old and grade A. We can execute the following SQL statement:
UPDATE students SET age=18, grade='A' WHERE name='张三';
This statement will update the name Zhang San in the students
table the age and grades of students.
Delete data
In MySQL, we use the DELETE statement to delete data in the table. The basic syntax of the delete statement is as follows:
DELETE FROM 表名 WHERE 条件;
Among them, table name
is the name of the table to delete data, and the WHERE
clause is used to specify the conditions for deleting data.
The following is a specific example:
Suppose we have a table named students
, containing the following fields: id
, name
, age
, grade
. Now we want to delete students who are younger than 18 years old. We can execute the following SQL statement:
DELETE FROM students WHERE age < 18;
This statement will delete students who are younger than 18 years old in the students
table.
Summary
By using SQL statements, we can easily update and delete data in MySQL. When updating data, use the UPDATE statement and specify the columns to be updated and the corresponding new values. You can also use the WHERE clause to specify the conditions for updating the data. When deleting data, use the DELETE statement and use the WHERE clause to specify the conditions for deleting data. These operations can help us better manage and maintain the data in the database.
The above is an introduction and code examples on how to use SQL statements to update and delete data in MySQL. Hope this article can be helpful to you!
The above is the detailed content of How to update and delete data in MySQL using SQL statements?. For more information, please follow other related articles on the PHP Chinese website!