SQL UPDATE Statement

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

SQL UPDATE statement

UPDATE statement is used to update a record that already exists in the table.


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 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 welookups sample database.

Here is the data from the "Websites" table:

| id | name         | url                       | alexa | country |
| 1  | Google       | https://www.google.com/ | 1     | USA     |
| 2  | Indiatimes          | https://www.Indiatimes.com/| 13    | IN      |
| 3  | zomato      | http://www.zomato.com/ | 4689  | IN      |
| 4  | indiamart          | http://indiamart.com/      | 20    | IN      |
| 5  | Facebook     | https://www.facebook.com/| 3     | USA     |

SQL UPDATE instance

Suppose we want to update the alexa ranking of "welookups Tutorial" to 5000 and the country to USA.

We use the following SQL statement:


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

Execute the above SQL, and then read the "Websites" table, the data is as follows:

Update Warning!

Be careful when updating records! In the example above, if we omit the WHERE clause, it looks like this:

UPDATE Websites
SET alexa = '5000', country = 'USA'

The above code will change the alexa of all data in the Websites table to 5000 and the country to USA.

Be careful with UPDATEs without WHERE clauses.