UPDATE Statement for Modifying Data
What you'll learn: How to change existing data in database tables using the UPDATE statement with SET and WHERE clauses.
What is UPDATE?
The UPDATE statement modifies existing rows in a table. Think of it like using an eraser and pencil to change entries in a ledger—you're not adding new lines or removing them, you're correcting or updating what's already there.
Core Structure
An UPDATE statement has three essential parts:
- UPDATE keyword plus the table name
- SET clause specifying which columns to change and their new values
- WHERE clause targeting specific rows (critical!)
UPDATE employees
SET salary = 65000
WHERE employee_id = 42;
This changes the salary to 65000 for the employee with ID 42—only that row.
The Danger of Missing WHERE
Without a WHERE clause, UPDATE affects every row in the table:
UPDATE employees
SET salary = 65000;
This would set everyone's salary to 65000—almost never what you want! Always double-check your WHERE conditions.
Updating Multiple Columns
You can modify several columns at once, separating them with commas:
UPDATE products
SET price = 29.99, stock_quantity = 150
WHERE product_id = 7;
Using Conditions for Targeting
Combine UPDATE with the filtering techniques you already know:
UPDATE orders
SET status = 'shipped'
WHERE order_date < '2024-01-01' AND status = 'pending';
Key Takeaway: The UPDATE statement modifies existing table rows using SET to specify new values and WHERE to target specific rows—always include WHERE unless you genuinely need to update every row.