SQL UPDATE statement
UPDATE statement is used to update records in a table.
SQL UPDATE statement
UPDATE statement to update the table in the existing record.
SQL UPDATE Syntax
UPDATE table_name
SET column1 = value1 , column2 = value2 ,...
WHERE some_column = some_value ;
SET column1 = value1 , column2 = value2 ,...
WHERE some_column = some_value ;
Note that SQL UPDATE statement WHERE clause! WHERE clause specifies which record or records which need to be updated. If you omit the WHERE clause, all records will be updated! |
The demo database
In this tutorial, we will use w3big sample database.
The following is a selected "Websites" table data:
+----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 本教程 | http://www.w3big.com/ | 4689 | CN | | 4 | 微博 | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | +----+--------------+---------------------------+-------+---------+
Examples of SQL UPDATE
Suppose we want to "tutorial" alexa ranking updated to 5000, country to USA.
We use the following SQL statement:
Examples
UPDATE Websites
SET alexa = '5000', country = 'USA'
WHERE name = 'tutorial';
Execute the above SQL, then read the "Websites" list, the data is as follows:
Update WARNING!
When updating records to be extra careful! In the example above, if we omit the WHERE clause as follows:
UPDATE Websites
SET alexa = '5000', country = 'USA'
SET alexa = '5000', country = 'USA'
Websites will execute the code above the table all the data alexa to 5000, country to USA.
UPDATE WHERE clause does not perform to be careful, then be careful.