How to Copy a Table in MySQL
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
This post covers how to copy a table in MySQL.
To copy the structure of an existing table, including its column definitions and indexes, but excluding the data, you can use the CREATE TABLE ... LIKE statement. This command clones the structure of the specified table into a new table.
CREATE TABLE new_table LIKE original_table;
Once you have the new table, you can fill it with data from the original table using the INSERT INTO ... SELECT syntax. This command fetches data from the original table and inserts it into the new one.
INSERT INTO new_table SELECT * FROM original_table;
Let’s say you have a table named employees and you wish to create a duplicate named employees_backup. First, create the structure of the backup table:
CREATE TABLE employees_backup LIKE employees;
Next, populate the employees_backup table with the data from the employees table:
INSERT INTO employees_backup SELECT * FROM employees;
This ensures you have an exact copy of the employees table, with all its rows, columns, and indexes intact. But remember that this doesn’t replicate foreign key constraints or triggers. You’d need to add those manually to the new table.
CREATE TABLE ... LIKE command replicates indexes, it doesn’t copy foreign key constraints. You should manually add these constraints to the new table if necessary.Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.