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
To copy with indexes and triggers do these 2 queries:
CREATE TABLE newtable LIKE oldtable;
INSERT INTO newtable SELECT * FROM oldtable;
To copy just structure and data use this one:
CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
I've asked this before:
Copy a MySQL table including indexes
MySQL way:
CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new SELECT * FROM production.recipes;
Try this :
`CREATE TABLE new-table (id INT(11) auto_increment primary key) SELECT old-table.name, old-table.group, old-table.floor, old-table.age from old-table;`
I selected 4 columns from old-table and made a new table.
Apart from the solution above, you can use AS
to make it in one line.
CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
I found the same situation and the approach which I took was as follows:
SHOW CREATE TABLE <table name to clone>
: This will give you the Create Table
syntax for the table which you want to cloneCREATE TABLE
query by changing the table name to clone the table.This will create exact replica of the table which you want to clone along with indexes. The only thing which you then need is to rename the indexes (if required).
Go to phpMyAdmin and select your original table then select "Operations" tab in the "Copy table to (database.table)" area. Select the database where you want to copy and add a name for your new table.