SQL UPDATEStatements


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, can be multiple fields.
  • value1, value2, ...: The values to modify, can be multiple values.
  • condition: The modification condition, used to specify which data to update.

lamp Please note the WHERE clause in the SQL UPDATE statement!
The WHERE clause specifies which record or records need to be updated. If you omit the WHERE clause, all records will be updated!


Demo Database

In this tutorial, we will use the EXAMPLE 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  | Example      | http://www.example.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 "Example" to 5000, and change the country to USA.

We use the following SQL statement:

Example

UPDATE Websites SET alexa='5000', country='USA' WHERE name='Example';

After executing the above SQL, read the "Websites" table again, the data is as follows:



Update Warning!

Be extra 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 of all records in the Websites table to 5000, and the country to USA.

Be cautious, be very cautious when executing an UPDATE without a WHERE clause.

Other Extensions