In MySQL, how to copy the content of one table to another table within the same database?

后端 未结 8 1284
既然无缘
既然无缘 2020-12-22 16:40

I am new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert to a table from another table.

相关标签:
8条回答
  • 2020-12-22 17:06
    CREATE TABLE target_table SELECT * FROM source_table;
    

    It just create a new table with same structure as of source table and also copy all rows from source_table into target_table.

    CREATE TABLE target_table SELECT * FROM source_table WHERE condition;
    

    If you need some rows to be copied into target_table, then apply a condition inside where clause

    0 讨论(0)
  • 2020-12-22 17:07

    If table1 is large and you don't want to lock it for the duration of the copy process, you can do a dump-and-load instead:

    CREATE TABLE table2 LIKE table1;
    
    SELECT * INTO OUTFILE '/tmp/table1.txt' FROM table1;
    LOAD DATA INFILE '/tmp/table1.txt' INTO TABLE table2;
    
    0 讨论(0)
  • 2020-12-22 17:10

    If you want to create and copy the content in a single shot, just use the SELECT:

    CREATE TABLE new_tbl SELECT * FROM orig_tbl;
    
    0 讨论(0)
  • 2020-12-22 17:11

    If the table doesn't exist, you can create one with the same schema like so:

    CREATE TABLE table2 LIKE table1;
    

    Then, to copy the data over:

    INSERT INTO table2 SELECT * FROM table1
    
    0 讨论(0)
  • 2020-12-22 17:15

    This worked for me,

    CREATE TABLE newtable LIKE oldtable;

    Replicates newtable with old table

    INSERT newtable SELECT * FROM oldtable;

    Copies all the row data to new table.

    Thank you

    0 讨论(0)
  • 2020-12-22 17:22

    Try this. Works well in my Oracle 10g,

    CREATE TABLE new_table
      AS (SELECT * FROM old_table);
    
    0 讨论(0)
提交回复
热议问题