Friday, April 29, 2011

Copy table query

For some requirements like backup, temp table creation we need to copy a table. In mysql we have the option " CREATE TABLE " to create a new table we can use this also for copy table.

Copy structure only:
Some time we only need the copy the table without data. here is query for that.

CREATE TABLE dest_table_name LIKE source_table_name;

This will create a new table like source table but without data.

Copy structure and data:
we can copy both data and structure via single query

CREATE TABLE dest_table_name SELECT * FROM source_table_name;

This create a new table with both data and structure.

Copy Data only:
if you have a table like another table , just need to copy the data only mean just use the "insert into" in mysql.

INSERT INTO dest_table_name select * from source_table_name;

No comments:

Post a Comment