SQL UPDATE Statement
The UPDATE statement is used to update records in a table.
SQL UPDATE Statement
The UPDATE statement is used to update existing records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Parameter Description:
- table_name: The name of the table to modify.
- column1, column2, ...: The names of the fields to modify; there can be multiple fields.
- value1, value2, ...: The values to modify; there can be multiple values.
- condition: The modification condition, used to specify which data to modify.
|
| Pay attention to the WHERE clause in the SQL UPDATE statement!
The WHERE clause specifies which record or records should be updated. If you omit the WHERE clause, all records will be updated! | | --- | --- |
Demonstration Database
In this tutorial, we will use the RUNOOB sample database.
Below is the data selected from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | Runoob Tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SQL UPDATE Example
Suppose we want to update the alexa ranking of "Runoob Tutorial" to 5000 and change the country to USA.
We use the following SQL statement:
Example
UPDATE
Websites
SET
alexa
=
'
5000
'
,
country
=
'
USA
'
WHERE
name
=
'
Runoob Tutorial
'
;
After executing the above SQL and reading the "Websites" table again, the data looks like this:
Update Warning!
Be especially careful when updating records! In the example above, if we omit the WHERE clause, as follows:
UPDATE Websites
SET alexa='5000', country='USA'
Executing the above code will change the alexa of all data in the Websites table to 5000 and the country to USA.
Be cautious, very cautious, when executing an UPDATE without a WHERE clause.