bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL UPDATE Statement

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind SQL UPDATE Statement?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ Customers
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The UPDATE statement is used to update or modify one or more records in a table.
UPDATE Multiple Records
The SQL UPDATE Statement

The SQL UPDATE Statement

The UPDATE statement is used to update or modify one or more records in a table.

UPDATE Syntax

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

Note

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!

Demo Database

Below is a selection from the Customers table used in the examples:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

UPDATE Table

The following SQL updates the record with CustomerID = 1, with a new contact person AND a new city.

Example

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

The selection from the "Customers" table will now look like this:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteAlfred SchmidtObere Str. 57Frankfurt12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

UPDATE Multiple Records

The WHERE clause determines which records that will be updated.

The following SQL will update the ContactName to "Juan" for ALL records where country is "Mexico":

Example

UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';

The selection from the "Customers" table will now look like this:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteAlfred SchmidtObere Str. 57Frankfurt12209Germany
2Ana Trujillo Emparedados y heladosJuanAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaJuanMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

Update Warning!

Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!

The following SQL will update the ContactName to "Juan" for ALL records:

Example

UPDATE Customers
SET ContactName='Juan';

The selection from the "Customers" table will now look like this:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteJuanObere Str. 57Frankfurt12209Germany
2Ana Trujillo Emparedados y heladosJuanAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaJuanMataderos 2312México D.F.05023Mexico
4Around the HornJuan120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpJuanBerguvsvägen 8LuleåS-958 22Sweden

Previous

SQL NULL Values

Next

SQL DELETE Statement