SQL UPDATE
Statement
The UPDATE statement is used to modify the existing 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 Explanation:
table_name: The name of the table to be modified.
column1, column2, ...: The names of the fields to be modified, which can be multiple fields.
value1, value2, ...: The new values to be set, which can be multiple values.
condition: The condition that specifies which records to update.
| | Please note the WHERE clause in the SQL UPDATE statement! <br>The WHERE clause specifies which record(s) need to be updated. If you omit the WHERE clause, all records will be updated! | | --- | --- |
Demonstration Database
In this tutorial, we will use the tutorialpro sample database.
Below is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | tutorialpro.org | http://www.tutorialpro.org/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SQL UPDATE Example
Suppose we want to update the alexa rank of "tutorialpro.org" to 5000 and change the country to USA.
We use the following SQL statement:
Example
UPDATE Websites
SET alexa='5000', country='USA'
WHERE name='tutorialpro.org';
After executing the above SQL, the data in the "Websites" table will look like this:
Update Warning!
Be very careful when updating records! In the above example, if we omit the WHERE clause, as shown below:
UPDATE Websites
SET alexa='5000', country='USA'
Executing the above code will change the alexa rank to 5000 and the country to USA for all records in the Websites table.
Exercise extreme caution when performing an UPDATE without a WHERE clause.