Merge two database into a single database

你说的曾经没有我的故事 提交于 2019-12-11 12:49:30

问题


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.



来源:https://stackoverflow.com/questions/20171956/merge-two-database-into-a-single-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!