SQL UPDATE

SQL UPDATE Statement

The UPDATE statement changes data values in a database.

UPDATE can update one or more records in a table.

Use the WHERE clause to UPDATE only specific records.

Example

#

Problem: Change the phone number for supplier Tokio Traders.

UPDATE Supplier
   SET Phone = '(03) 8888-5011'
 WHERE CompanyName = 'Tokyo Traders'

Syntax

UPDATE syntax.

UPDATE table-name 
   SET column-name1 = value1, 
       column-name2 = value2, ...

UPDATE syntax with a WHERE clause.

UPDATE table-name 
   SET column-name1 = value1, 
       column-name2 = value2, ...
 WHERE condition

More Examples

SQL UPDATE

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
Problem: Discontinue all products in the database.
UPDATE Product
   SET IsDiscontinued = 1
Note: A value of 1 denotes true.
Result:  77 records updated.

SQL UPDATE WHERE

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
Problem: Discontinue all products over $50.
UPDATE Product
   SET IsDiscontinued = 1
 WHERE UnitPrice > 50
Note: the value 1 denotes true.
Result:  7 records updated.

SQL UPDATE Single Record

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
Problem: Discontinue the product with Id = 46.
UPDATE Product
   SET IsDiscontinued = 1
 WHERE Id = 46
Note: This is a more common scenario in which a single record is updated.
Result:  1 record updated.

SQL UPDATE Multiple Columns

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
Problem: Supplier Norske Meierier (Id = 15) has moved.
Change their city, phone, and fax with updated values.
UPDATE Supplier
   SET City = 'Oslo', 
       Phone = '(0)1-953530', 
       Fax = '(0)1-953555'
 WHERE Id = 15
Note: This is a common scenario in which a single record is updated.
Result:  1 record updated.

You may also like



Guides