SQL UPDATE statement usage (Single Column and multiple columns), sqlupdate
Update statement
The Update statement is used to modify data in a table.
Syntax:
UPDATE
Table Name SET
Column name = New ValueWHERE
Column name = A Value
For example:
Person table:
LastName |
FirstName |
Address |
City |
Gates |
Bill |
Xuanwumen 10 |
Beijing |
Wilson |
|
Champs-Elysees |
|
Update a column in a row
We add firstname for the person whose lastname is "Wilson:
UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'
Result:
LastName |
FirstName |
Address |
City |
Gates |
Bill |
Xuanwumen 10 |
Beijing |
Wilson |
Fred |
Champs-Elysees |
|
Update several columns in a row
We will modify the address and add the city name ):
UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing'WHERE LastName = 'Wilson'
Result:
LastName |
FirstName |
Address |
City |
Gates |
Bill |
Xuanwumen 10 |
Beijing |
Wilson |
Fred |
Zhongshan 23 |
Nanjing |
SQL update statement update (update table data)
1. Update all rows in the table
2. When updating a specific row in the table, do not omit the WHERE clause. Otherwise, all rows will be updated.
The update statement is divided into three parts. The format is as follows:
1. Updated table
2. Column name and New Value
3. Determine which rows to update.
Single Column:
UPDATE CustomersSET cust_email = ' kim@qq.com'WHERE cust_id = '10000005';
Multiple columns:
UPDATE CustomersSET cust_email = 'kim@qq.com' , cust_contact ='Sam Roberts'WHERE cust_id = '10000005';
When updating multiple columns, you only need to use a SET command. Each "column = value" pair is separated by a comma, And the last column is not separated by a comma.
Update warning!
Be extremely careful when updating records! In the above example, if we omit the WHERE clause, as shown below:
UPDATE CustomersSET cust_email = 'kim@qq.com'
Executing the code above will change the cust_email for all the data in the MERs table to the kim@qq.com.
Exercise caution when executing an UPDATE without a WHERE clause. Generally, this is done only after batch replacement.
Small editor for helping customers: we strongly recommend that you back up the data before proceeding.