SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

SQL UPDATE



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


SQL UPDATE statement

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

SQL UPDATE syntax

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

lampPlease pay attention to 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 php sample database.

The following is the data selected from the "Websites" table:

+----+--------------+--- ------------------------+------+---------+
| id | name | url                                                                    --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 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 ranking of "php Chinese website" to 5000. Change country to USA.

We use the following SQL statement:

Example

UPDATE Websites
SET alexa='5000', country='USA'
WHERE name='php中文网';
Execute the above SQL, and then read the "Websites" table. The data is as follows:


##Update Warning!

Be extremely careful when updating records! In the above example, if we omit the WHERE clause as follows:

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


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

Be careful when executing an UPDATE without a WHERE clause, and be more careful.