SQL UPDATE statement usage (Single Column and multiple columns), sqlupdate

Source: Internet
Author: User

SQL UPDATE statement usage (Single Column and multiple columns), sqlupdate

Update statement

The Update statement is used to modify data in a table.

Syntax:

UPDATETable Name SET Column name = New ValueWHEREColumn 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.