SQL UPDATE


SQL
POSTGRES
INTERACTIVE

Complete SQL UPDATE Technical Guide

1. Fundamental Syntax
UPDATE table_name
SET column1 = value1,
    column2 = value2
[WHERE conditions];

Key Notes:

  1. WHERE clause is optional but critical for targeted updates
  2. Can update multiple columns in one statement
  3. Values can be literals, expressions, or subqueries
2. Advanced Update Patterns
Conditional Updates
UPDATE products
SET price = CASE
    WHEN stock > 100 THEN price * 0.9
    WHEN stock < 10 THEN price * 1.1
    ELSE price
  END;
From Subquery
UPDATE employees e
SET salary = (
  SELECT AVG(salary)
  FROM employees
  WHERE department = e.department
);
Join Updates
UPDATE orders o
JOIN customers c ON o.customer_id = c.id
SET o.priority = 'HIGH'
WHERE c.status = 'VIP';
Incremental Updates
UPDATE page_views
SET view_count = view_count + 1
WHERE page_id = 123;
3. Database-Specific Features
PostgreSQL
UPDATE ... RETURNING *;
-- Returns updated rows
FROM additional_tables
-- Powerful FROM clause
SQL Server
UPDATE TOP (100) table...
-- Limits rows updated
OUTPUT inserted.*
-- Captures changes
MySQL
UPDATE IGNORE table...
-- Skips errors
LIMIT row_count
-- Controls update volume
Oracle
RETURNING column INTO variable
-- Captures single value
MERGE statement
-- Upsert capability
4. Performance Considerations
Index Usage
  1. WHERE clause should use indexed columns
  2. Updating indexed columns is expensive
  3. Consider disabling indexes during bulk updates
Locking Behavior
  1. Row-level vs table-level locks
  2. Transaction isolation levels matter
  3. Long-running updates block other operations
Batch Updates
-- SQL Server pattern
WHILE EXISTS (SELECT 1 FROM table WHERE needs_update = 1)
BEGIN
  UPDATE TOP (1000) table...
END
CTE Updates
WITH targets AS (
  SELECT id FROM large_table
  WHERE condition LIMIT 10000
)
UPDATE large_table
SET column = value
FROM targets
WHERE large_table.id = targets.id;
5. Error Handling & Safety
Common Pitfalls
  1. Missing WHERE clause updates all rows
  2. Constraint violations (FK, CHECK, UNIQUE)
  3. Data type conversion errors
Safety Measures
  1. Use transactions for multi-statement updates
  2. Test with SELECT first
  3. Backup before large updates
  4. Add LIMIT clauses for safety
Transaction Pattern
BEGIN TRANSACTION;

-- First verify with SELECT
SELECT * FROM table WHERE condition;

-- Then update
UPDATE table SET column = value WHERE condition;

-- Commit only after verification
COMMIT;
-- or ROLLBACK if issues found
6. Advanced Scenarios
JSON Updates
-- PostgreSQL
UPDATE users
SET profile = jsonb_set(profile, '{address,city}', '"New York"')
WHERE id = 123;
Temporal Tables
-- SQL Server
UPDATE Employees
SET Position = 'Manager'
WHERE EmployeeID = 123;
-- Automatically preserves history
Partitioned Tables
UPDATE sales PARTITION (p_2023)
SET status = 'archived'
WHERE sale_date BETWEEN '2023-01-01' AND '2023-12-31';
Update with Window Functions
WITH ranked AS (
  SELECT id, RANK() OVER (PARTITION BY dept ORDER BY sales DESC) as rnk
  FROM salespeople
)
UPDATE salespeople
SET bonus = CASE WHEN rnk = 1 THEN 5000 ELSE 2000 END
FROM ranked
WHERE salespeople.id = ranked.id;

Advanced UPDATE Topics

1. UPDATE Triggers
CREATE TRIGGER log_salary_changes
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
  INSERT INTO salary_audit
  VALUES (OLD.employee_id, OLD.salary, NEW.salary, CURRENT_TIMESTAMP);
END;
  1. BEFORE UPDATE: Executes before the update operation, can modify values
  2. AFTER UPDATE: Executes after successful updates, often used for auditing
  3. INSTEAD OF UPDATE: (For views) Replaces the standard update operation
  4. Access to OLD/NEW values: OLD contains pre-update values, NEW contains proposed changes
2. Replication Impact
-- MySQL binary log format examples
SET binlog_format = 'ROW';  -- Logs changed rows only
SET binlog_format = 'STATEMENT';  -- Logs the actual UPDATE statement
  1. Row-based replication: Efficient for large updates but larger log size
  2. Statement-based replication: Compact logs but non-deterministic updates can cause drift
  3. Conflict resolution: Multi-master replication requires strategies for concurrent updates
  4. Latency considerations: Large updates may cause replication lag
3. Materialized View Refresh
-- PostgreSQL example
UPDATE inventory SET stock = stock - 10 WHERE product_id = 123;

-- Manual refresh needed for some materialized views
REFRESH MATERIALIZED VIEW CONCURRENTLY inventory_summary;
  1. Immediate vs deferred refresh: Some views update automatically, others require manual refresh
  2. Incremental refresh: Only recomputes affected portions of the view
  3. Performance impact: Base table updates may slow down with many dependent views
  4. Transaction boundaries: Some systems refresh views at transaction commit
4. Distributed Updates
-- Cassandra batch update example
BEGIN BATCH
  UPDATE users SET email = 'new@email.com' WHERE user_id = 1;
  UPDATE profiles SET verified = true WHERE user_id = 1;
APPLY BATCH;
  1. Two-phase commit: Coordinated updates across shards with prepare/commit phases
  2. Paxos protocols: For consensus in distributed systems
  3. Shard routing: Updates must reach the correct partition
  4. Eventual consistency: Some systems allow temporary inconsistencies
  5. Conflict-free Replicated Data Types (CRDTs): Special data structures for distributed updates
5. UPDATE Execution Plans
-- PostgreSQL EXPLAIN output example
EXPLAIN UPDATE orders
SET status = 'processed'
WHERE order_date < '2023-01-01';

-- Typical plan:
Update on orders (cost=0.29..1254.32 rows=4231 width=86)
  -> Index Scan using idx_order_date on orders (cost=0.29..1254.32 rows=4231 width=86)
        Index Cond: (order_date < '2023-01-01'::date)
  1. Read-modify-write cycle: How the planner breaks down the operation
  2. Index usage: For WHERE clause vs updated columns
  3. Join strategies: In multi-table updates
  4. Statistics dependence: Cardinality estimates affect plan choice
  5. Parallel update: Some databases can parallelize certain updates
6. Concurrency Control (MVCC)
-- PostgreSQL MVCC visibility example
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Other transactions see old value until commit
COMMIT;
  1. Version chains: How databases store multiple row versions
  2. Transaction isolation levels: READ UNCOMMITTED to SERIALIZABLE
  3. Write skew: Special concurrency issue with certain update patterns
  4. Predicate locking: For SERIALIZABLE isolation
  5. Hot spots: Contention on frequently updated rows