SQL UPDATE statement

The UPDATE statement is used to update the records in the table.

SQL UPDATE statement

The UPDATE statement is used to update an existing record in the table.

SQL UPDATE syntax

UPDATE table_name
SET column1 = value1 , column2 = value2 ,…
WHERE some_column = some_value ;


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



Demo database

In this tutorial, we will use the RUNOOB sample database.
The following is the data selected 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 | rookie tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | microblogging | http://weibo.com/ | 20 | CN |
5 | Facebook | https://www.facebook.com/ | 3 | USA |
+ ---- + -------------- + --------------------------- + - ------ + --------- +



SQL UPDATE instance

Suppose we want to update the “rookie tutorial” alexa ranking to 5000, country changed to USA.
We use the following SQL statement:

Examples

UPDATE Websites SET Alexa = 5000 , country = USA WHERE Name = rookie tutorial ;
Execute the above SQL, and then read the “Websites” table, the data is as follows:



Update Warning!

Be extra careful when updating your records! In the above example, if we omit the WHERE clause, as follows:
UPDATE Websites
SET alexa = ‘5000’, country = ‘USA’ 
The above code will change all the data in the Websites table alexa to 5000, country to USA.
Execute UPDATE without WHERE clause to be careful and cautious.

Leave a Reply

Your email address will not be published. Required fields are marked *