Duplicating a MySQL table, indices, and data

前端 未结 12 1206
天涯浪人
天涯浪人 2020-11-27 08:42

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

相关标签:
12条回答
  • 2020-11-27 09:13

    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

    0 讨论(0)
  • 2020-11-27 09:14

    MySQL way:

    CREATE TABLE recipes_new LIKE production.recipes;
    INSERT recipes_new SELECT * FROM production.recipes;
    
    0 讨论(0)
  • 2020-11-27 09:14

    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.

    0 讨论(0)
  • 2020-11-27 09:15

    Apart from the solution above, you can use AS to make it in one line.

    CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
    
    0 讨论(0)
  • 2020-11-27 09:18

    I found the same situation and the approach which I took was as follows:

    1. Execute SHOW CREATE TABLE <table name to clone> : This will give you the Create Table syntax for the table which you want to clone
    2. Run the CREATE 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).

    0 讨论(0)
  • 2020-11-27 09:19

    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.

    0 讨论(0)
提交回复
热议问题