Swapping column values in MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Swapping two column values in MySQL sounds simple, but a naive UPDATE can produce the wrong result. The reason is assignment order: in a single-table UPDATE, later assignments may see values that were already changed earlier in the same statement. A safe swap therefore needs either a temporary value or a query shape that preserves the original values explicitly.
Why the Naive Update Fails
Suppose the table has first_name and last_name and you try:
This looks like a swap, but it is not reliable as a swap pattern in MySQL. Once first_name is updated, the second assignment can end up seeing the changed value rather than the original one. The result is often both columns ending up the same.
That is why swap logic needs to preserve the old values somewhere.
Safe Pattern: Temporary Table Snapshot
A clear and reliable approach is to capture the old values, then update from that snapshot.
This is verbose, but it is easy to reason about because the update reads from stable copied values rather than values being changed in place.
Safe Pattern for Many Rows
The same idea works for more than one row. For example, to swap two columns for every employee in one department:
This keeps the update scoped and avoids accidental modification of unrelated rows.
Numeric Columns May Use Arithmetic Tricks, but Be Careful
For numeric columns only, people sometimes use arithmetic swaps such as:
This can work mathematically, but it is usually a poor default because:
- it only fits numeric data
- it is easy to misunderstand later
- large values can introduce overflow risk
For production SQL, clarity is usually worth more than cleverness.
Use Transactions for Safety
If the swap matters, run it inside a transaction so you can verify the result before committing.
During development or one-off admin work, replacing COMMIT with ROLLBACK for a dry run is often a good habit.
Test the Scope First
Before running the UPDATE, inspect the target rows with the same filter used by the swap logic.
This sounds basic, but many destructive SQL mistakes come from getting the WHERE clause wrong, not from the swap logic itself.
Know When an Application-Layer Fix Is Better
If the swap is part of one-time cleanup, SQL is fine. If it is application behavior that happens regularly, it may be easier to express the intent in application code and then persist the corrected values. The database is good at set-based updates, but not every recurring business rule is best encoded as clever SQL.
Common Pitfalls
- Using
SET col1 = col2, col2 = col1and assuming MySQL will preserve the original values automatically. - Running a swap without checking the affected rows first.
- Using arithmetic swaps for nonnumeric or potentially overflowing values.
- Forgetting to wrap an important bulk update in a transaction.
- Choosing a short but opaque trick instead of a slightly longer query that is easier to verify.
Summary
- A naive two-assignment
UPDATEis not a safe general swap pattern in MySQL. - The reliable approach is to preserve the original values explicitly before updating.
- Temporary-table or snapshot-based swaps are clear and generic.
- Arithmetic tricks are limited and usually less maintainable.
- Use transactions and verify the target rows before running bulk swaps.
Related reading
- Switch Master and Slave role in mysql
- Sync in Android sqlite and sql server crud operation in two ways
- Synchronize two postgresql databases with current data using with bucardo
- Synchronizing data from MSSQL to Elasticsearch using Apache Kafka
- Synchronizing keyspaces in new cassandra datacenter
- Synchronous vs. asynchronous database access
- Syncing/Streaming MySQL Table/TablesJoined Tables with PostgreSQL Table/Tables
- Table 'DBNAME.hibernate_sequence' doesn't exist

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.