Here is the issue. I have an app that was installed on two different servers with DB 1 and DB2. Each has the same schema and auto incremental key. but DB2 has more data input than DB1 while DB1 has some important data that is not in DB2.
How can I merge the two databases without losing my data in any of them?
I have searched and come across a guide that suggested this
INSERT INTO DB1.TABLE1 SELECT * FROM DB2.TABLE1
But my problem is that the databases have up to 106 tables.
is there a way to merge all the tables at once?
No, there is no easy button tool or procedure to merge rows from tables from two schemas.
We can envision the special case of a table that is guaranteed not to have any conflicting rows, where rows can be simply copied from one table to the other.
But in the more general case (a typical entity-relationship model implemented as tables in a relational database), we can't avoid data conflict issues due to UNIQUE KEY constraints, FOREIGN KEY constraints, et al.
For examle,
If table (id,name)
in DB1 has row (3,'amigos')
and in DB2 has row (3,'blind mice')
When we attempt to INSERT the row from DB2 into the table in DB1, and we get a unique key constraint violation on id=3, how do we handle that? Do we just not copy the row from DB2? Do we modify the id
value on the row from DB2 to be (1113,'blind mice')
when its copied into DB1?
How we handle these types of issues will depend. Is this a lookup table that is relatively static (a "domain" or "list of values" table), if the contents of the tables are the same in both databases, we would expect the contents of the merged table to be the same.
But we will handle that differently for a table of transactions where we expect the merged table to contain all the rows from DB1 and all the rows from DB2.
Q: "Is there a way to merge all the tables at once?"
A: No. If there was a tool do that kind of merge (if it did happen to do the right thing in a special case), with no doubt it will do the wrong thing on some of the 100+ tables.
This is all depends on how your auto incrementing keys are setup and how normalized/relational your data is. The more normalized your data is, the harder it will be. This isn't exactly an answer, but a general idea. This isn't an easy way, but probably the easiest .
This has the added advantage of giving you an easy way to segregate the old data from each database from the new data for trouble shooting in the future.
This is the easiest way to keep keys and foreign keys consistent without lots of lots of custom code.
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.