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

SqlMysqlDatabase

Sql Problem Overview


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. Is there easy way of doing this?

Sql Solutions


Solution 1 - Sql

If the tables have the same structure:

INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE;

If the tables have different structures:

INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;

You can also add conditions:

INSERT INTO TARGET_TABLE (`col1_`,`col2_`) SELECT `col1`,`col2` FROM SOURCE_TABLE WHERE `foo`=1

Solution 2 - Sql

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

Solution 3 - Sql

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;

Solution 4 - Sql

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.

Solution 5 - Sql

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;

Solution 6 - Sql

This worked for me. You can make the SELECT statement more complex, with WHERE and LIMIT clauses.

First duplicate your large table (without the data), run the following query, and then truncate the larger table.

INSERT INTO table_small (SELECT * FROM table_large WHERE column = 'value' LIMIT 100)

Super simple. :-)

Solution 7 - Sql

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

Solution 8 - Sql

Try this. Works well in my Oracle 10g,

CREATE TABLE new_table
  AS (SELECT * FROM old_table);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoneph O.View Question on Stackoverflow
Solution 1 - SqlggirouxView Answer on Stackoverflow
Solution 2 - SqlGStoView Answer on Stackoverflow
Solution 3 - SqlIke WalkerView Answer on Stackoverflow
Solution 4 - SqlJasonView Answer on Stackoverflow
Solution 5 - SqlFrank HeikensView Answer on Stackoverflow
Solution 6 - SqlSandor FeketeView Answer on Stackoverflow
Solution 7 - SqljisnaView Answer on Stackoverflow
Solution 8 - SqlRAHUL KUMARView Answer on Stackoverflow