SQL UPDATE Statement
How do I update values in a database?
The UPDATE statement updates data values in a database.
UPDATE can update one or more records in a table.
Use the WHERE clause to UPDATE only specific records.
The SQL UPDATE syntax
The general syntax is
UPDATE table-name SET column-name1 = value1, column-name2 = value2, ...
To limit the number of records to UPDATE append a WHERE clause:
UPDATE table-name SET column-name1 = value1, column-name2 = value2, ... WHERE condition
PRODUCT |
---|
Id |
ProductName |
SupplierId |
UnitPrice |
Package |
IsDiscontinued |
SQL UPDATE Examples
Problem: Discontinue all products in the database
UPDATE Product SET IsDiscontinued = 1
Note: the value 1 denotes true.
Result: 77 records updated.
PRODUCT |
---|
Id |
ProductName |
SupplierId |
UnitPrice |
Package |
IsDiscontinued |
Problem: Discontinue products over $50.
UPDATE Product SET IsDiscontinued = 1 WHERE UnitPrice > 50
Note: the value 1 denotes true.
Result: 7 records updated.
PRODUCT |
---|
Id |
ProductName |
SupplierId |
UnitPrice |
Package |
IsDiscontinued |
Problem: Discontinue product with Id = 46.
UPDATE Product SET IsDiscontinued = 1 WHERE Id = 46
This is a more common scenario in which a single record is updated.
Note: the value 1 denotes true.
Note: the value 1 denotes true.
Result: 1 record updated.
SUPPLIER |
---|
Id |
CompanyName |
ContactName |
City |
Country |
Phone |
Fax |
Problem: Supplier Norske Meierier (Id = 15) has moved:
update their city, phone and fax.
update their city, phone and fax.
UPDATE Supplier SET City = 'Oslo', Phone = '(0)1-953530', Fax = '(0)1-953555' WHERE Id = 15
This is a common scenario in which a single record is updated.
Result: 1 record updated.