SQL UPDATE

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

Warning: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!


Syntax

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

Demo Database

Below is a selection from the Customers table:

Customers Table:

CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Emparedados Ana Trujillo Mexico
3 Antonio Moreno Taquería Antonio Moreno Mexico

Example 1: Update a Single Record

The following SQL statement updates the first customer (CustomerID = 1) with a new contact name.

UPDATE Example

UPDATE Customers
SET ContactName = 'Alfred Schmidt'
WHERE CustomerID = 1;

After the update, the table would look like this:

CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Alfred Schmidt Germany
2 Ana Trujillo Emparedados Ana Trujillo Mexico
3 Antonio Moreno Taquería Antonio Moreno Mexico

Example 2: Update Multiple Records

The WHERE clause determines how many records will be updated.

The following SQL statement will update the Country to "United States" for all records where the Country is currently "Mexico".

UPDATE Multiple Records Example

UPDATE Customers
SET Country = 'United States'
WHERE Country = 'Mexico';

Example 3: Update All Records (Dangerous!)

If you omit the WHERE clause, all records will be updated.

-- This will set the Country to "USA" for ALL customers
UPDATE Customers
SET Country = 'USA';