Duplicate a Table in MySQL

You can duplicate a MySQL table by copying its rows, creating a new table from its definition, or combining both operations. The correct method depends on whether the duplicate must contain data, indexes, constraints, triggers, or only selected rows and columns.

The two commonly used approaches are:

  1. Use CREATE TABLE ... AS SELECT to create a table from query results and copy data in one statement.
  2. Use CREATE TABLE ... LIKE followed by INSERT ... SELECT to copy the supported table definition and indexes first, and then copy the rows.

These approaches do not create an identical copy of every database object. MySQL does not automatically copy triggers with either method. In addition, CREATE TABLE ... LIKE does not copy foreign key definitions. Review those objects separately when an exact schema duplicate is required.

MySQL Table Duplication Methods Compared

Duplication requirementRecommended MySQL statementImportant limitation
Copy columns and rows in one operationCREATE TABLE new_table AS SELECT * FROM old_table;Indexes, primary keys, triggers, foreign keys, auto-increment behavior, and some column attributes are not reproduced as a complete schema definition.
Copy table structure without rowsCREATE TABLE new_table LIKE old_table;The new table is empty, and triggers and foreign key definitions are not copied.
Copy structure, indexes, and rowsCREATE TABLE ... LIKE, followed by INSERT ... SELECTTriggers, foreign keys, grants, and the auto-increment value still require verification.
Copy selected rows or columnsUse a column list and a WHERE clauseSource and destination column types and order must be compatible.
Copy a table to another databaseUse database-qualified table namesThe destination database must exist, and the account must have the required privileges.

Duplicate MySQL Table Structure and Data with CREATE TABLE AS SELECT

A table’s structure includes its columns and properties such as data types, character sets, and default values. The records stored in those columns are the table’s data.

To create a new table from the columns and rows returned by another table, use CREATE TABLE ... AS SELECT. Following is the syntax:

</>
Copy
CREATE TABLE new_table AS SELECT * FROM old_table;

This statement creates the destination table and inserts the selected rows in one operation. It is suitable for temporary analysis tables, filtered copies, and data snapshots where an exact copy of every index and constraint is not required.

Do not use this method as an exact schema-cloning operation. Compare the source and destination definitions when primary keys, unique indexes, defaults, auto-increment columns, generated columns, triggers, or foreign keys are required.

Example: Duplicate the students Table as pupils

Following is the students table’s structure.

Describe MySQL Table structure

And students table has the following data.

show mysql table data

Now we shall copy the structure and data of students into a new table pupils.

MySQL Duplicate Table with Structure and Data

Let us see if both duplicate and original tables exist.

MySQL SHOW TABLES

Both table names should now appear. Next, compare the destination table’s columns and rows with the source table.

MySQL Duplicated Table Structure
MySQL Duplicated Table Data

The rows have been copied to pupils. Use SHOW CREATE TABLE when you also need to compare indexes, constraints, defaults, and other table attributes.

Duplicate Only the MySQL Table Structure Without Data

Use CREATE TABLE ... LIKE when you need a new empty table based on an existing table definition:

</>
Copy
CREATE TABLE new_table LIKE old_table;

For example, the following statement creates an empty pupils table based on students:

</>
Copy
CREATE TABLE pupils LIKE students;

The new table receives the supported column definitions and indexes, but it does not receive any rows. Triggers and foreign key definitions are also not copied automatically.

Duplicate MySQL Table Structure, Indexes, and Data

To duplicate a MySQL table’s structure and indexes, and then copy its data, run two SQL statements in the MySQL command-line interface or another SQL client.

</>
Copy
CREATE TABLE new_table LIKE old_table; 
INSERT new_table SELECT * FROM old_table;
  • The first query creates an empty table based on the old table’s supported definition and indexes.
  • The second query copies the rows from the old table to the new table.
  • Triggers are not copied automatically and must be recreated separately.
  • Foreign key definitions are not copied by CREATE TABLE ... LIKE and must be added separately when required.

To check the effect on index, we shall add a new column to mysql table students and try duplicating the table.

students table structure

MySQL after adding a new column to the table

students table index

MySQL show index of table

Now we shall copy the table definition, indexes, and data into a new table called pupils.

Run the following two queries.

</>
Copy
CREATE TABLE pupils LIKE students; 
INSERT pupils SELECT * FROM students;
MySQL Copy table with index

Let us see what happened to the index of newly created table pupils.

MySQL Index of Duplicated Table

For application and production scripts, an explicit column list is safer than SELECT *. It documents the intended column mapping and avoids accidental mismatches if a table definition changes.

</>
Copy
INSERT INTO pupils (student_id, student_name, class_name)
SELECT student_id, student_name, class_name
FROM students;

Duplicate Selected MySQL Rows or Columns

Add a column list and a WHERE condition when the new table should contain only part of the source data. The following statement creates a table containing active students:

</>
Copy
CREATE TABLE active_students AS
SELECT student_id, student_name, class_name
FROM students
WHERE status = 'active';

When the destination should retain the supported source structure and indexes, create it with LIKE first and then insert the filtered rows:

</>
Copy
CREATE TABLE active_students LIKE students;

INSERT INTO active_students
SELECT *
FROM students
WHERE status = 'active';

Copy a MySQL Table to Another Database

Use database-qualified table names to duplicate a table into another database on the same MySQL server. The destination database must already exist.

</>
Copy
CREATE TABLE archive.students_copy LIKE school.students;

INSERT INTO archive.students_copy
SELECT *
FROM school.students;

The MySQL account must be able to read the source table and create and populate the destination table. Triggers, foreign keys, routines, events, and table-specific grants require separate review.

Duplicate a Table in MySQL Workbench

MySQL Workbench can run the same duplication statements used in the command-line client. Open a SQL Editor tab for the required connection and execute the following example:

</>
Copy
USE school;

CREATE TABLE pupils LIKE students;

INSERT INTO pupils
SELECT *
FROM students;
  1. Select the correct schema in the Navigator or run USE database_name;.
  2. Run CREATE TABLE ... LIKE to create the destination table.
  3. Run INSERT ... SELECT to copy the rows.
  4. Refresh the Schemas panel and inspect the newly created table.
  5. Compare its definition, indexes, row count, triggers, and foreign keys with the source table.

Recreate MySQL Triggers for the Duplicated Table

MySQL triggers belong to a specific table and are not copied by CREATE TABLE ... LIKE or CREATE TABLE ... AS SELECT. List the source table’s triggers before duplicating it:

</>
Copy
SHOW TRIGGERS
WHERE `Table` = 'students';

Retrieve the definition of each required trigger before adapting it for the new table:

</>
Copy
SHOW CREATE TRIGGER trigger_name;

Create the destination triggers with unique trigger names and the correct destination table. Consider whether the triggers should exist before or after the initial data copy, because an active destination trigger can run for every inserted row.

Verify the Duplicated MySQL Table

Do not verify the operation only by checking that the new table name exists. Compare the source and destination definitions, indexes, triggers, and row counts:

</>
Copy
SHOW CREATE TABLE students;
SHOW CREATE TABLE pupils;

SHOW INDEX FROM students;
SHOW INDEX FROM pupils;

SELECT COUNT(*) AS source_rows FROM students;
SELECT COUNT(*) AS copied_rows FROM pupils;

SHOW TRIGGERS
WHERE `Table` IN ('students', 'pupils');

Also test representative reads and writes against the new table. If the source table receives updates while data is being copied, choose an appropriate transaction or maintenance strategy so that the duplicate represents the intended point in time.

Common MySQL Table Duplication Errors

MySQL duplication problemLikely causeWhat to check
Destination table already existsCREATE TABLE requires an unused table nameChoose a different name or remove the existing destination only after confirming that it is no longer needed.
Duplicate-key error during the insertThe destination contains values that conflict with a primary key or unique indexInspect existing destination rows and decide whether to clear, merge, update, or exclude conflicting records.
Column-count or data-type mismatchSELECT * does not match the destination tableSpecify matching source and destination column lists in the same order.
Missing trigger on the duplicateTriggers are not copied automaticallyInspect the source triggers and recreate the required definitions for the destination table.
Missing foreign keyCREATE TABLE ... LIKE does not copy foreign key definitionsReview SHOW CREATE TABLE and add the required constraints after validating the copied data.
Unexpected next auto-increment valueThe copied definition and inserted identifiers do not leave the required sequence valueInspect the destination table and set the next auto-increment value explicitly when necessary.

MySQL Duplicate Table Editorial QA Checklist

  • Confirm that the tutorial distinguishes CREATE TABLE ... AS SELECT from CREATE TABLE ... LIKE.
  • Confirm that it does not claim that MySQL automatically copies triggers or foreign key definitions.
  • Verify that source and destination table names remain consistent throughout each SQL example.
  • Check that examples using SELECT * explain when an explicit column list is safer.
  • Verify the duplicated table with SHOW CREATE TABLE, SHOW INDEX, trigger checks, and row counts.
  • Check permissions and database-qualified names before copying a table across databases.
  • Review the destination table’s auto-increment value and representative read and write operations.

MySQL Duplicate Table FAQs

How do I duplicate a MySQL table with all its data?

Run CREATE TABLE new_table LIKE old_table; and then INSERT INTO new_table SELECT * FROM old_table;. This copies the supported table definition and indexes before copying the rows. Review triggers and foreign keys separately.

How do I duplicate a MySQL table without copying its data?

Use CREATE TABLE new_table LIKE old_table;. The resulting table is empty. Triggers and foreign key definitions are not copied automatically.

Does CREATE TABLE AS SELECT copy indexes and primary keys?

It creates columns from the query result and copies the selected rows, but it should not be used when an exact reproduction of indexes, primary keys, defaults, auto-increment behavior, triggers, foreign keys, and other schema details is required.

Does MySQL copy triggers when duplicating a table?

No. Inspect the source table’s triggers with SHOW TRIGGERS and retrieve individual definitions with SHOW CREATE TRIGGER. Adapt and recreate only the triggers required for the destination table.

Can I duplicate a MySQL table into another database?

Yes, when both databases are on the same MySQL server and the account has suitable privileges. Use qualified names such as archive.students_copy and school.students in the CREATE TABLE and INSERT ... SELECT statements.

MySQL Table Duplication Summary

In this MySQL Tutorial, we learned to duplicate a table using CREATE TABLE ... AS SELECT and CREATE TABLE ... LIKE with INSERT ... SELECT. Use the first method for query-based copies and the second when the duplicate should retain the supported table definition and indexes. Verify triggers, foreign keys, grants, row counts, and auto-increment behavior separately.