In MySQL, how to copy the content of one table to another table within the same database?
To quickly replicate all data from existing_table
to new_table
, with matching structures, use INSERT INTO...SELECT:
If new_table
has different columns, specify these and their matching sources:
Handling different data scenarios
Selective Data Transfer - Using WHERE clause
Need to copy only some rows? The WHERE
clause is your friend!
Exact Structure Clone - CREATE TABLE LIKE
Want to an exact structure clone without data like a showroom piece? Here you go:
Both structure and data copying in one go
For cloning structure and data simultaneously, check this out:
Complex table structures? No problem!
Custom column mapping
For differently structured tables, align the column names:
Ensuring data integrity and efficiency
Stay smart! Preserve data integrity and efficiency by observing foreign keys, indexes, and constraints.
Advanced considerations for pros
On-the-fly data transformation
Modify data during the copy process using SQL functions:
Handling bulky data transfers
For massive data, consider row chunking, batch inserts, or even temporary tables.
Taking care of auto-increment values
When dealing with auto-increment columns, take care to properly manage them, or they might bump into each other!
Was this article helpful?