本文介绍了将两个数据库合并到单个数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有两个从不同的mysql服务器转储相同的数据库。我需要通过调整主键和外键自动将这两个转储合并到一个数据库。 例如,考虑两个sql转储,即mysqldump1和mysqldump2,它们包含两个表country_table和child_table。以下是两个转储的数据: $ 转储:mysqldump1.dmp Table1:country_table / p> + ---- + ------------------- + | id |国家| + ---- + ------------------- | | 1 |印度| | 2 |中国| | 3 | USA | | 4 |英格兰| | 5 |伊朗| + ---- + ------------------- + Table2:person_table + ---- + --------- + ------------- + | id |名称|国家(fk)| + ---- + ------------------- | ------------- | | 1 |汤姆| 1 | | 2 | Anish | 2 | | 3 |詹姆斯| 2 | | 4 | Akhil | 5 | | 5 | Jobi | 4 | + ---- + ------------------- + ------------- + 转储:mysqldump2.dmp Table1:country_table + ---- + ------------------- + | id |国家| + ---- + ------------------- | | 1 |迪拜| | 2 |澳大利亚| + ---- + ------------------- + Table2:person_table + ---- + --------- + ------------- + | id |名称|国家(fk)| + ---- + ------------------- | ------------- | | 1 | Binu | 1 | | 2 | Justin | 2 | | 3 |马克| 2 | + ---- + ------------------- + ------------- + 结果数据库包含两个转储的条目组合,如下所示。请注意,结果数据库包含调整主键和外键的上述两个数据库的组合$ b $b¨ 结果数据库 country_table + ---- + -------------- ----- + | id |国家| + ---- + ------------------- | | 1 |印度| | 2 |中国| | 3 | USA | | 4 |英格兰| | 5 |伊朗| | 6 |迪拜| | 7 |澳大利亚| + ---- + ------------------- + person_table + ---- + ------------ ------- + ------------- + | id |名称|国家(fk)| + ---- + ------------------- | ------------- | | 1 |汤姆| 1 | | 2 | Anish | 2 | | 3 |詹姆斯| 2 | | 4 | Akhil | 5 | | 5 | Jobi | 4 | | 6 | Binu | 6 | | 7 | Justin | 7 | | 8 |马克| 7 | + ---- + ------------------- + ------------- + 你能建议我把两个数据库合并到单个数据库,如上所示。 (我使用mysql数据库)解决方案将每个表导出为CSV,然后将它们导入合并的数据库。一旦您拥有同一数据库中的所有表,就可以编写SQL来附加数据。 请参阅 MYSQL - 连接两个表以获取关于连接部分的更多详细信息。 同一数据库。 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 dumpsDump: mysqldump1.dmpTable1: 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.dmpTable1: 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 keysResult Databasecountry_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) 解决方案 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. 这篇关于将两个数据库合并到单个数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 20:12