How do I copy or clone or duplicate the data, structure, and indices of a MySQL table to a new one?
This is what I've found so far.
This will copy the data and the structure, but not the indices:
create table {new_table} select * from {old_table};
This will copy the structure and indices, but not the data:
create table {new_table} like {old_table};
You can duplicate or "clone" a table's contents by executing a CREATE TABLE ... AS SELECT statement: CREATE TABLE new_table AS SELECT * FROM original_table; Please be careful when using this to clone big tables.
Use SHOW CREATE TABLE to get a CREATE TABLE statement that specifies the source table's structure, indexes and all. Modify the statement to change the table name to that of the clone table and execute the statement. This way, you will have the exact clone table.
To copy with indexes and triggers do these 2 queries:
CREATE TABLE new_table LIKE old_table; INSERT INTO new_table SELECT * FROM old_table;
To copy just structure and data use this one:
CREATE TABLE new_table AS SELECT * FROM old_table;
I've asked this before:
Copy a MySQL table including indexes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With