简体   繁体   中英

MySQL Import data from two different database to one database Or merge two database data

I have two databases i need to merge both database in one database. for example

let_client1 (same fields and table number that on let_client2 database)

let_client2

I have 99 tables on both databases, also 5 views in both databases.

There are many indexing, primary key and foreign key relations so how could i merge both database in one database. is there any best method in Mysql?

As a starting point you can use an insert into select statement as such:

INSERT INTO let_client1.table1 (field1, field2, etc)
SELECT field1, field2, etc
FROM let_client2.table1

This will merge the data from let_client2.table1 into let_client1.table1, however beware of primary key violations and foreign key restrictions so you will need to perform this operation on the tables that have dependants first.

If you want a purely automatic solution I would suggest using cursors to select the tables and pass their names into a procedure that does the merging, but you will easily run into the issues I just described above. http://dev.mysql.com/doc/refman/5.7/en/cursors.html

As for the indexes, if they need transferring, here's a related question already with answers: MySql, how can I export indexes from my development database to my production database?

There's no single easy method to do this, because it depends on the table definitions.

  • If you use auto-increment primary keys, your two databases might both be using the same values. These would have to be adjusted somehow.

  • Foreign key constraints require your tables to be loaded in a particular order. Or else you could use SET FOREIGN_KEY_CHECKS=0 while you do the data load.

I think you'll have to write a script to do the data merging in a way that works for your specific case.

You can use the INFORMATION_SCHEMA to get a list of tables and a list of foreign key constraints.

You don't have to transfer indexes or views. They will both be populated as you load data into your tables. MySQL views do not store data, they only query the base tables.

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