MySQL UPDATE
Modifying existing records in database tables
✏️ What is UPDATE?
The UPDATE statement modifies existing records in a table. You can change one or multiple columns and use WHERE clause to specify which rows to update. Always use WHERE to avoid updating all rows accidentally.
-- Update a user's email
UPDATE users
SET email = '[email protected]'
WHERE id = 1;
UPDATE Operations
Single Column
Update one field at a time
Multiple Columns
Change several fields together
Conditional Update
Update based on conditions
Safety First
Always use WHERE clause
🔹 Basic UPDATE Syntax
The UPDATE statement requires the table name, SET clause with new values, and typically a WHERE clause to specify which rows to modify. Without WHERE, all rows will be updated. Always double-check your WHERE condition before executing updates to prevent accidental data changes across your entire table.
-- Update single column for one user
UPDATE users
SET email = '[email protected]'
WHERE id = 1;
-- Update with name condition
UPDATE users
SET age = 26
WHERE name = 'John Doe';
Result:
✓ Query OK, 1 row affected
1 record updated successfully
🔹 Updating Multiple Columns
You can update multiple columns in a single UPDATE statement by separating them with commas in the SET clause. This is more efficient than running multiple UPDATE statements. It ensures all changes happen together atomically, maintaining data consistency and reducing database load for better performance and reliability.
-- Update multiple fields at once
UPDATE users
SET email = '[email protected]',
age = 31,
city = 'New York'
WHERE id = 2;
-- Update product details
UPDATE products
SET price = 899.99,
stock = 75,
last_updated = NOW()
WHERE product_id = 10;
Result:
✓ Query OK, 1 row affected
Multiple columns updated for 1 record
🔹 UPDATE with Conditions
Use WHERE clause with various conditions to update specific rows. You can combine multiple conditions using AND, OR, and comparison operators. This allows precise targeting of records that need modification. Complex WHERE clauses enable sophisticated update operations based on multiple criteria for flexible data management.
-- Update based on multiple conditions
UPDATE products
SET discount = 10
WHERE category = 'Electronics' AND price > 500;
-- Update with range condition
UPDATE users
SET membership = 'Premium'
WHERE age BETWEEN 25 AND 40 AND country = 'USA';
-- Update using pattern matching
UPDATE products
SET featured = TRUE
WHERE product_name LIKE '%Pro%';
Result:
✓ Query OK, 15 rows affected
15 records matched the conditions and were updated
🔹 UPDATE with Calculations
You can use mathematical expressions and functions in the SET clause to calculate new values based on existing data. This is useful for incrementing counters, applying percentage changes, or performing calculations. It allows dynamic updates where new values depend on current values, enabling powerful data transformations.
-- Increase price by 10%
UPDATE products
SET price = price * 1.10
WHERE category = 'Books';
-- Increment view count
UPDATE articles
SET views = views + 1
WHERE id = 5;
-- Calculate discount price
UPDATE products
SET sale_price = price - (price * discount / 100)
WHERE discount > 0;
Result:
✓ Query OK, 23 rows affected
Prices increased by 10% for 23 products
🔹 UPDATE with LIMIT
The LIMIT clause restricts the number of rows updated, which is useful for batch processing or testing updates on a small subset first. This provides an extra safety layer when updating large datasets. Use LIMIT to update records gradually or to test your UPDATE statement before applying it to all matching rows.
-- Update only first 10 matching rows
UPDATE users
SET status = 'Active'
WHERE last_login > '2024-01-01'
LIMIT 10;
-- Test update on single row first
UPDATE products
SET price = price * 0.9
WHERE category = 'Clearance'
LIMIT 1;
Result:
✓ Query OK, 10 rows affected
Limited to 10 rows as specified
🔹 UPDATE Safety Tips
Always follow best practices when updating data to prevent accidental data loss or corruption. Test your WHERE clause with SELECT first, use transactions for critical updates, and always backup important data before major changes. Careful UPDATE operations protect your database integrity and prevent costly mistakes that could affect your entire system.
Safety Guidelines:
- Always use WHERE: Without it, ALL rows will be updated
- Test with SELECT first: Run SELECT with same WHERE to see what will be updated
- Use transactions: Wrap critical updates in BEGIN/COMMIT
- Backup first: Create backups before major updates
- Check affected rows: Verify the number of rows changed
- Use LIMIT for testing: Test on small subset first
-- ⚠️ DANGER: Updates ALL rows (no WHERE clause)
UPDATE users SET status = 'Inactive';
-- ✅ SAFE: Test with SELECT first
SELECT * FROM users WHERE last_login < '2023-01-01';
-- ✅ Then UPDATE with same WHERE
UPDATE users
SET status = 'Inactive'
WHERE last_login < '2023-01-01';