MySQL How to copy rows, but change a few fields?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The standard MySQL way to copy rows while changing some values is INSERT ... SELECT. You select existing data from one or more rows, replace the fields you want to change with literals or expressions, and insert the result as new rows.
Basic Pattern
Suppose you have an orders table and want to copy one order while resetting its status and timestamp. The pattern looks like this:
The important detail is that you list the destination columns explicitly. You usually leave out the auto-increment primary key so MySQL can generate a new one.
Copy Multiple Rows with Changes
You are not limited to a single source row. INSERT ... SELECT can duplicate many rows at once.
Here the copied rows keep most of their original values, but:
- the due date moves forward by seven days
- the priority is reset
- the version number is incremented
This is much cleaner than selecting rows into application code and re-inserting them one at a time.
Copy Between Tables
The same technique works across different tables as long as the selected columns match the insert target.
The source and destination do not have to share the same schema exactly. You can reshape the result as part of the SELECT.
Why Explicit Column Lists Matter
Never rely on INSERT INTO table SELECT * FROM table ... for this kind of operation. It is fragile because:
- column order can change later
- generated columns may not behave the way you expect
- primary keys and unique columns often need special handling
Explicit column lists make the copy safer and easier to review.
Think About Constraints Before Running the Query
Copying rows can fail if the new rows violate:
- primary key uniqueness
- unique indexes
- foreign key constraints
- application-level assumptions about timestamps or status values
For example, if the table has a unique slug column, copying a row without changing that field will fail. In that case, change the duplicated value in the SELECT:
Use Transactions for Safety
When the copy is part of a larger business operation, wrap it in a transaction.
That makes it easier to roll back if later steps fail.
Common Pitfalls
- Copying the primary key column and causing a duplicate-key error.
- Using
SELECT *instead of listing columns explicitly. - Forgetting to modify unique fields that must stay distinct.
- Copying rows from the same table without a restrictive
WHEREclause and creating far more duplicates than intended. - Running a large duplication query outside a transaction when related writes must stay consistent.
Summary
- Use
INSERT ... SELECTto copy rows while changing selected fields. - Omit auto-increment keys unless you intentionally want to control them.
- Replace fields in the
SELECTwith literals, expressions, or MySQL functions. - Always list destination columns explicitly.
- Check uniqueness and constraint rules before duplicating data.
Related reading
- MySQL how to join tables on two fields
- MySQL How to modify stored procedures atomically?
- MySQL IF NOT NULL, then display 1, else display 0
- MySQL ignore errors when importing?
- MySQL IN condition limit
- MySQL Incorrect datetime value '0000-00-00 000000
- MySQL incorrect string value error when save unicode string in Django
- MySQL indexes - what are the best practices?

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.