Merge two database into a single database

2019-08-14 02:27发布

问题:

I have two dump of same database from different mysql servers. i need to combine these two dumps into a single database automatically by adjusting the primary keys and foreign keys.

For example consider two sql dumps say mysqldump1 and mysqldump2 which contains two tables country_table and child_table. Here follows the data of two dumps

Dump: mysqldump1.dmp
Table1: country_table

+----+-------------------+
| id |    Country        |
+----+-------------------|
|  1 |  India            |
|  2 |  China            |
|  3 |  USA              |
|  4 |  England          |
|  5 |  Iran             |
+----+-------------------+

Table2: person_table

+----+-------------------+-------------+
| id |    Name           | Country (fk)|
+----+-------------------|-------------|
|  1 |  Tom              |     1       |
|  2 |  Anish            |     2       |
|  3 |  James            |     2       |
|  4 |  Akhil            |     5       |
|  5 |  Jobi             |     4       |
+----+-------------------+-------------+

Dump: mysqldump2.dmp
Table1: country_table

+----+-------------------+
| id |    Country        |
+----+-------------------|
|  1 |  Dubai            |
|  2 |  Australia        |
+----+-------------------+

Table2: person_table

+----+-------------------+-------------+
| id |    Name           | Country (fk)|
+----+-------------------|-------------|
|  1 |  Binu             |     1       |
|  2 |  Justin           |     2       |
|  3 |  Mark             |     2       |
+----+-------------------+-------------+

The result database contains contains entries combination of two dumps which looks like follows. Please note that the result database which contains the combination of above two databases which adjusts the primary and foreign keys
Result Database
country_table

+----+-------------------+
| id |    Country        |
+----+-------------------|
|  1 |  India            |
|  2 |  China            |
|  3 |  USA              |
|  4 |  England          |
|  5 |  Iran             |
|  6 |  Dubai            |
|  7 |  Australia        |
+----+-------------------+

person_table

+----+-------------------+-------------+
| id |    Name           | Country (fk)|
+----+-------------------|-------------|
|  1 |  Tom              |     1       |
|  2 |  Anish            |     2       |
|  3 |  James            |     2       |
|  4 |  Akhil            |     5       |
|  5 |  Jobi             |     4       |
|  6 |  Binu             |     6       |
|  7 |  Justin           |     7       |
|  8 |  Mark             |     7       |
+----+-------------------+-------------+

Can you please suggest me an idea to merge two database into single database which looks like above. (I am using mysql database)

回答1:

Export each table to CSV, then import them into the merged database. Once you have all the tables in the same database you can write SQL to append the data.

See MYSQL - Concatenate two tables for more details about the concatenating part.

The trick is to get them in the same database first.