How to duplicate a table with keys & other structure features retained in MySQL?

后端 未结 3 423
无人共我
无人共我 2021-02-03 10:42

How to duplicate a table with keys & other structure features retained? including primary key, foreign keys, and indexes.

Can this be done with a single MySQL query?

相关标签:
3条回答
  • 2021-02-03 11:12

    Following query creates and duplicates data.

    CREATE TABLE table2 SELECT * FROM table1

    0 讨论(0)
  • 2021-02-03 11:22

    you can do it in this query

    CREATE TABLE a LIKE b
    

    after you can insert

    INSERT INTO a SELECT * FROM b
    

    read more information in this article

    0 讨论(0)
  • 2021-02-03 11:25

    duplicating a table from another table (with indexing and structure)cannot be done with a single query you will need need 2 queries.

    1) To create Duplicate table.

    CREATE TABLE Table2 LIKE Table1;

    This will create an exact copy of the table.

    2) Fill in the Duplicate table with values from original table.

    INSERT INTO Table2 SELECT * from Table1;

    will fill Table2 with all the records fom Table1

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