MySQL Copy a Table Including Attributes

There is more than one way to copy a table in MySQL, but if you want to include column attributes and indexes defined in the original table you’ve got to choose the correct one!

Copy Table Via Select

This first method of copying a table will insert the data but will not replicate column attributes and indexes defined in the original table:

CREATE TABLE table2 SELECT * FROM table1;

Copy Table Using Like

This second method of copying a table uses the LIKE keyword, and will not insert the data, but will create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:

CREATE TABLE table2 LIKE table1;

And then it’s a simple matter of inserting the data:

INSERT INTO table2 SELECT * FROM table1;

Useful Links

Leave a Reply

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