MySQL How to copy rows, but change a few fields?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

