The UPDATE statement is used to update existing records in a table. It is very important to use the WHERE clause in combination with the UPDATE statement. One or more fields can be referenced.
If you OMIT the WHERE clause, all records in the table will be updated!
SQL UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
|
SQL UPDATE Example
UPDATE Customers
SET State='Georgia', City='Atlanta'
WHERE LastName='Smith' AND FirstName='John'
|
Advanced Example using SELECT in combination with UPDATE
You may wish to update records in one table based on values in another table. Since you can't list more than one table in the UPDATE statement, you can use the EXISTS clause.
UPDATE Marketing
SET MCustName = ( SELECT Customers.Name
FROM Customers
WHERE Customers.CustID = Marketing.CustID)
WHERE EXISTS
( SELECT Customers.Name
FROM Customers
WHERE Customers.CustID = Marketing.CustID) |
Whenever a Marketing.CustID is matched with a Customers.CustID value, the MCustName would be updated with the customer's name from the Customers table.
Please help us spread the word by socializing it today!
Did you find something wrong with the information on this page? Please take a moment to report it to us
so that we can continue to improve the quality of the information on this site. Click here to
report an issue with this page.
Recommended Books & Training Resources