How to duplicate a MySQL table, including indexes and data?

There are MySQL GUI and tools that makes table duplication easy. But sometimes, you wish to copy table from CLI. This post is about doing it from CLI.

So, to copy everything, including database objects such as indexes, primary key constraint, foreign key constraints, triggers, etc., run these queries:

CREATE TABLE new_table LIKE old_table; // copies table structure and indices
INSERT new_table SELECT * FROM old_table; // copies data into new_table

Leave a Reply

Your email address will not be published. Required fields are marked *