How to copy data from one table to another new table in MySQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Copying data into a new MySQL table can mean two different things: creating a new table with copied rows, or cloning the original table structure and then inserting the data. The right choice depends on whether you need indexes, defaults, and constraints copied along with the data.
Fastest Way: CREATE TABLE ... AS SELECT
If you just want a new table populated from a query result, use:
This creates archived_orders and fills it in one statement.
It is convenient, but it does not fully clone the original table definition. You usually do not get the original indexes, foreign keys, or all column metadata exactly as before.
Best Way When Structure Matters
If you want the new table to preserve the existing table structure more accurately, use CREATE TABLE ... LIKE first:
Then copy the data:
This is usually the safer production approach because the new table starts with the same column definitions and indexes as the source table.
Copy Only Specific Columns
If the destination table has a different shape, list the columns explicitly:
Explicit column lists are important when:
- Column order differs
- Some columns should be skipped
- The new table only stores a subset of the old data
Transform During the Copy
The copy query can also transform the data:
This is useful for reporting tables, archives, or denormalized snapshots.
Structure Copying Is Not All-Or-Nothing
This is the important distinction:
- '
CREATE TABLE ... AS SELECTis convenient for data shape produced by a query' - '
CREATE TABLE ... LIKEis better when you care about the original schema details'
Even with LIKE, you should still confirm whether things such as triggers or environment-specific permissions need separate handling in your workflow. Schema copying and data copying are related, but they are not identical concerns.
Use Transactions Carefully
For large tables, copying can take time and lock resources depending on the storage engine and workload. If consistency matters, think about:
- Running during a maintenance window
- Copying in batches
- Using transactions where appropriate
For example, batch inserts can reduce operational risk:
Then repeat for the next range.
Common Pitfalls
- Using
CREATE TABLE ... AS SELECTand expecting indexes and constraints to be preserved automatically. - Using
SELECT *when the destination schema may change later. - Forgetting to filter rows and copying far more data than intended.
- Running a huge copy operation without considering locks, transaction size, or available disk space.
Summary
- Use
CREATE TABLE ... AS SELECTfor quick query-result copies. - Use
CREATE TABLE ... LIKEplusINSERT INTO ... SELECTwhen structure fidelity matters. - List columns explicitly when schemas differ or when you only need part of the data.
- MySQL lets you transform data during the copy, not just duplicate it.
- The safest method depends on whether schema preservation or convenience is more important.

