MySQL Copy Table from One Database to Another

October 1, 2008

if you want to copy a table from one Database to another database, You can simply do as below.
CREATE TABLE db2.table LIKE db1.table;
INSERT INTO db2.table SELECT * FROM db1.table;

Example:
mysql> drop table user;
Query OK, 0 rows affected (0.12 sec)

mysql> show tables;
+—————————+
| Tables_in_mysql_copy |
+—————————+
| columns_priv |
| db |
| func |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| proc |
| procs_priv |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
+—————————+
16 rows in set (0.00 sec)

mysql> create TABLE mysql_copy.user LIKE mysql.user;
Query OK, 0 rows affected (0.12 sec)

mysql> show tables;
+—————————+
| Tables_in_mysql_copy |
+—————————+
| columns_priv |
| db |
| func |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| proc |
| procs_priv |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+—————————+
17 rows in set (0.00 sec)

mysql> select * from user;
Empty set (0.03 sec)

mysql> INSERT into mysql_copy.user select * from mysql.user;
Query OK, 8 rows affected (0.11 sec)
Records: 8 Duplicates: 0 Warnings:0