SQL UPDATE Statement

The UPDATE statement in SQL is used to modify existing data within a table. It lets you change values in one or more columns for selected rows.



UPDATE: Definition and Usage

UPDATE is a core command for maintaining database accuracy. It's crucial to always include a WHERE clause to specify exactly which rows to modify; otherwise, the entire table will be updated, potentially leading to significant data loss. This is because the `UPDATE` statement will change every row in the table if no `WHERE` clause is specified.

Syntax

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
      

Example Database

Here's a sample 'Customers' table to illustrate:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

Examples

Updating a Single Record

This updates the contact name and city for CustomerID 1.

Syntax

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
      
Output
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

Updating Multiple Records

This updates the 'ContactName' for all Mexican customers.

Syntax

UPDATE Customers
SET ContactName = 'Juan'
WHERE Country = 'Mexico';
      
Output
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany
2 Ana Trujillo Emparedados y helados Juan Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Juan Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

Updating All Records (Caution!)

This example shows what happens when you omit the WHERE clause—all records are updated! This is dangerous and should be avoided unless you intend to change every single row in the table.

Syntax

UPDATE Customers
SET ContactName = 'Juan';
      
Output
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Juan Obere Str. 57 Frankfurt 12209 Germany
2 Ana Trujillo Emparedados y helados Juan Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Juan Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Juan 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Juan Berguvsvägen 8 Luleå S-958 22 Sweden