MySQL Update

The UPDATE statement modifies existing records in a table, and pairing it with a WHERE clause is essential to avoid rewriting every row by mistake.

The UPDATE Statement Syntax

UPDATE is used to change the values of one or more columns in rows that already exist in a table. The basic syntax names the table, uses SET to assign new values to one or more columns, and typically uses WHERE to restrict which rows are affected.

Basic UPDATE syntax

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

Updating a Single Row

The most common use of UPDATE is to correct or refresh a single record, such as fixing a customer's contact name after they call in to report a change.

Update one customer's contact name and city

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

Updating Multiple Rows

A WHERE clause is not limited to matching a single primary key — it can use any condition, so a single UPDATE statement can adjust many rows at once, for example giving every customer in a particular city a corrected postal code prefix.

Update every customer located in Mexico

UPDATE Customers
SET PostalCode = '05021'
WHERE Country = 'Mexico';
Note: UPDATE without a WHERE clause changes every single row in the table. Always double-check your WHERE clause — ideally by running the equivalent SELECT first — before executing an UPDATE.

What Happens If You Omit WHERE

If the WHERE clause is omitted entirely, MySQL will happily apply the SET assignments to every row in the table, which is rarely the intent. This is one of the most common and most damaging mistakes made by beginners, since the change applies instantly and affects the entire dataset.

Dangerous: this updates ALL rows in the table

UPDATE Customers
SET City = 'Berlin';
Note: Best practice: run SELECT * FROM Customers WHERE Country = 'Mexico'; first to confirm exactly which rows will be affected, then convert it to an UPDATE once you're confident.

Updating Several Columns Together

You can assign new values to multiple columns in a single statement by separating each column = value pair with a comma inside the SET clause.

  • Always pair UPDATE with WHERE unless you truly intend to change every row
  • Multiple columns can be set in one statement, separated by commas
  • Test your filter with a SELECT before converting it to an UPDATE
  • Most database tools run in autocommit mode, so an UPDATE takes effect immediately
ClausePurpose
UPDATE table_nameNames the table to modify
SET col = valueSpecifies the new value(s) to write
WHERE conditionRestricts which rows are affected

Exercise: MySQL Update

What is the purpose of the UPDATE statement?