Skip to content

By mastering the UPDATE statement in MySQL, you can efficiently modify existing records to reflect new information or correct errors. But how exactly can you leverage the UPDATE statement? This journey will explore various application scenarios.

What is UPDATE in MySQL?

To modify records in MySQL, use the UPDATE statement as follows:

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

In this structure:

  • Specify the table_name where the modifications will occur.
  • Use SET to list the columns that need updating along with their new values.
  • Apply the WHERE clause to target the specific records for updating. Without this, you risk updating every record in the table.

How to update a single record?

For updating a unique record, ensure your condition uniquely identifies it. For instance, to change the email of a user with a specific user_id:

UPDATE users
SET email = '[email protected]'
WHERE user_id = 1;

How to update multiple records?

To modify several records simultaneously, define a broader condition. For example, to activate all users residing in ‘New York’:

UPDATE users
SET status = 'active'
WHERE city = 'New York';

Update with caution

It is crucial to specify your WHERE clause accurately to avoid unwanted updates. Before executing an UPDATE, perform a SELECT query with the same conditions to review the targeted records.

Using joins in updates

Update records using values from another table by incorporating a JOIN. For example, to change the status of users based on their payment status:

UPDATE users u
JOIN payments p ON u.user_id = p.user_id
SET u.status = 'verified'
WHERE p.payment_status = 'completed';

Here, the JOIN combines data from both users and payments tables, enabling updates based on related information across tables.

Effective use of the UPDATE statement can significantly enhance your database management. Always ensure accuracy in your conditions and double-check your statements to maintain data integrity.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.