简体   繁体   中英

Merge two database into a single database

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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