Sunday, 12 February 2012

Copy table between two Databases

Code:   
CREATE TABLE DB2.new_copy LIKE DB1.master_copy;
INSERT INTO DB2.new_copy SELECT * FROM DB1.master_copy;


The first line creates the table using the same engine as the existing table and creates the primary keys and/or indexes.
The second line copies the data across.

If the original table has many keys then it may help to speed the INSERT if you turn off the keys using the following before the INSERT:

Code:   
ALTER TABLE new_table_name DISABLE KEYS;
   
And then after the INSERT:

Code:   
ALTER TABLE new_table_name ENABLE KEYS;
   
These two statements are not supported by all MySQL Engine types though. InnoDB being one of them in MySQL ver 5.x



No comments: