This article describes how to use the UPDATE and DELETE statements in MySQL. it is the basic knowledge in MySQL beginners. For more information, see
How to use the UPDATE and DELETE statements in MySQL _ MySQL
UPDATE
The update set syntax is used to modify and UPDATE data in a data table.
Syntax:
UPDATE tb_name SET column1 = new_value1,column2 = new_value2,… WHERE definition
This syntax updates the value of column1 in the record that meets the WHERE condition in the data table to new_value1, and the value of column2 to new_value2, and so on. If the WHERE condition is omitted, the column values of all records in the table are updated.
Example:
Data before update:
In this example, the username in the user table is changed to Xiaoming's email as xiaoming@163.com.
Updated data:
UPDATE expression
The UPDATE syntax allows a SET followed by an expression.
Example 1:
UPDATE article SET pv = pv+1 WHERE id = 123
This example adds 1 to the clicks of an article with the id of 123 when it is clicked for reading.
Example 2:
UPDATE persondata SET age = age*2, age = age+1
In this example, the expression "SET" is followed by age = age * 2 (double age), age = age + 1 (plus 1 ). These expressions are executed from left to right.
Delete From Delete data
DELETE
The delete from syntax is used to DELETE data records of a data table.
Syntax:
DELETE FROM tb_name WHERE definition
This syntax deletes data records that meet the WHERE condition in the data table. If the WHERE condition is omitted, all records in the table are deleted.
Example:
The data is successfully deleted, and the browser outputs:
Delete one data record.
Data before deletion:
Deleted data:
If no matching record is deleted, but mysql_query () still returns TRUE (unless the SQL syntax is incorrect ). Therefore, to accurately determine whether a data record is deleted, call the mysql_affected_rows () function (this function returns the number of rows affected by the last INSERT, UPDATE, or DELETE query ).
Prompt
If you only want to delete the field data of a record, use the update set syntax to leave it blank.
The above is the use of the UPDATE and DELETE statements in MySQL tutorial _ MySQL content. For more information, please follow the PHP Chinese network (www.php1.cn )!