65

I can copy a MySQL table to create a new table:

CREATE TABLE newtable SELECT * FROM oldtable

This works, but the indexes are not copied to the new table. How can I copy a table including the indexes?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • `indexes not create` vs `prevent the indexes` is ambiguous, what do you want to do? – Pentium10 Mar 10 '10 at 09:52
  • 5
    possible duplicate of [Duplicating a MySQL table, indexes and data](http://stackoverflow.com/questions/3280006/duplicating-a-mysql-table-indexes-and-data) – luchaninov Oct 01 '14 at 18:41
  • 4
    @luchaninov the answer on that question mentions this question. This is an infinite loop. – FrancescoMM Sep 20 '17 at 08:21

1 Answers1

129

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;
raveren
  • 17,799
  • 12
  • 70
  • 83