简体   繁体   English

将两个数据库合并为一个数据库

[英]Merge two database into a single database

I have two dump of same database from different mysql servers. 我有两个来自不同mysql服务器的相同数据库的转储。 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. 例如,考虑两个sql转储,分别是mysqldump1和mysqldump2,其中包含两个表country_table和child_table。 Here follows the data of two dumps 以下是两个转储的数据

Dump: mysqldump1.dmp 转储:mysqldump1.dmp
Table1: country_table 表1:country_table

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

Table2: person_table 表2:person_table

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

Dump: mysqldump2.dmp 转储:mysqldump2.dmp
Table1: country_table 表1:country_table

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

Table2: person_table 表2: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 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) (我正在使用mysql数据库)

Export each table to CSV, then import them into the merged database. 将每个表导出为CSV,然后将它们导入到合并的数据库中。 Once you have all the tables in the same database you can write SQL to append the data. 将所有表放在同一个数据库中后,就可以编写SQL来追加数据。

See MYSQL - Concatenate two tables for more details about the concatenating part. 有关连接部分的更多详细信息,请参见MYSQL-连接两个表

The trick is to get them in the same database first. 诀窍是先将它们保存在同一数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM