In MySQL, can I copy one row to insert into the same table?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Yes — MySQL supports duplicating a row within the same table using INSERT INTO ... SELECT syntax. The main challenge is handling the auto-increment primary key, which must be unique. You either explicitly list all columns except the primary key, or use a temporary variable to exclude it. This technique is useful for creating template records, duplicating configuration rows, or setting up test data.
Basic Row Duplication
By listing all columns except id in the INSERT and SELECT, MySQL generates a new auto-increment value for the copy. The original row is unchanged.
Copying with a Modified Column
You can modify any column in the SELECT clause while copying the rest. This is common for creating variants (like a discounted version of a product) or clearly marking duplicates.
Using a Temporary Table
This approach works when the table has many columns and you want to avoid listing them all. However, it is more complex and not commonly needed for simple duplication.
Listing All Columns Dynamically
In stored procedures or application code, you can query INFORMATION_SCHEMA to dynamically build the column list, avoiding manual updates when the table schema changes.
Duplicating with Unique Constraints
If the table has unique constraints beyond the primary key (like email), you must modify those columns during duplication to avoid constraint violations.
Copying Multiple Rows
The WHERE clause in SELECT controls which rows are duplicated. You can copy one row, a filtered subset, or the entire table.
Application Code (Python)
This dynamically fetches non-primary-key columns, making the duplication code resilient to schema changes.
Stored Procedure for Row Duplication
A stored procedure encapsulates the duplication logic and returns the new row's ID. This keeps the logic in the database and simplifies application code.
Common Pitfalls
- Including the auto-increment column in SELECT:
INSERT INTO products SELECT * FROM products WHERE id = 1fails with a duplicate key error because it tries to insert the sameid. Always exclude the primary key column from both INSERT and SELECT. - Forgetting unique constraints: Copying a row with a unique email or unique name column causes a constraint violation. Modify unique columns in the SELECT (e.g.,
CONCAT('copy_', email)). - Trigger side effects: If the table has INSERT triggers (for logging, auditing, or cascading), duplicating a row fires those triggers. This may create unintended audit records or notifications.
- Foreign key references: If other tables reference the original row by its
id, the duplicate gets a newidand those references do not automatically copy. You may need to duplicate related rows in child tables as well. - created_at and updated_at columns: Columns with
DEFAULT CURRENT_TIMESTAMPget the current time in the copy, not the original timestamp. If you want to preserve the original timestamp, explicitly include it in the SELECT list.
Summary
- Use
INSERT INTO table (cols) SELECT cols FROM table WHERE id = Nto duplicate a row - Exclude the auto-increment primary key to let MySQL assign a new ID
- Modify unique columns in the SELECT to avoid constraint violations
- Use
CONCAT(name, ' (Copy)')or similar to distinguish duplicates - Query
INFORMATION_SCHEMA.COLUMNSto dynamically build the column list - Use
LAST_INSERT_ID()to get the new row's auto-generated ID
Related reading
- In MySQL, how to copy the content of one table to another table within the same database?
- In MySQL what does Overhead mean, what is bad about it, and how to fix it?
- In PHP with PDO, how to check the final SQL parametrized query?
- In Python, how do I index a list with another list?
- In Redshift/Postgres, how to count rows that meet a condition?
- IN statement in dynamodb
- In Tensorflow, how to assign values in Tensor according to the indices?
- In which case do you use the JPA JoinTable annotation?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.