How to update rows
The syntax of the UPDATE
statement
UPDATE table_name
SET column_name_1 = expression_1 [, column_name_2 = expression_2]...
[WHERE search_condition]
Update one column of one row
UPDATE products
SET discountPercent = '10.00'
WHERE productName = 'Fender Telecaster';
Update multiple columns of one row
UPDATE products
SET discountPercent = '25.00',
description = 'This guitar has great tone and smooth playability.'
WHERE productName = 'Fender Telecaster';
Update one column of multiple rows
UPDATE products SET discountPercent = '15.00'
WHERE categoryID = 2;
Update one column of all rows in the table
UPDATE products SET discountPercent = '15.00';
Use a subquery to update multiple rows
UPDATE orders SET shipAmount = 0
WHERE customerID IN (
SELECT customerID FROM customers WHERE lastName = 'Sherwood'
);
Description
- You use the
UPDATE
statement to modify one or more rows in the table named in the UPDATE
clause.
- You name the columns to be modified and the value to be assigned to each column in the SET clause. You can specify the value for a column as a literal or an expression.
- You can specify the conditions that must be met for a row to be updated in the WHERE clause.
Warning
- If you omit the WHERE clause, all rows in the table are updated.
Back