简体   繁体   English

合并来自不同服务器中两个数据库的两个表

[英]Merge two tables from two databases in different servers

I am developing a mobile app that will be able to work in offline mode.我正在开发一个能够在离线模式下工作的移动应用程序。

I have an online application that consumes a table called "Books" in a database located on a server, and my mobile app has a sqlite database with the exactly table "Books".我有一个在线应用程序,它在位于服务器上的数据库中使用一个名为“Books”的表,而我的移动应用程序有一个 sqlite 数据库,该数据库恰好是“Books”表。

When the app gets internet avaible, it syncs the data from "Books" table - located in sqlite - to "Books" table - located in SQL Server.当应用程序可以上网时,它会将数据从“书籍”表 - 位于 sqlite - 同步到“书籍”表 - 位于 SQL 服务器中。

I am using the MERGE (Transact-SQL) statement to perform it.我正在使用MERGE (Transact-SQL)语句来执行它。 But I have a problem.但我有一个问题。

Imagine my mobile app is without internet connection for 7 days and the tables are:想象一下,我的移动应用程序 7 天没有互联网连接,表格是:

Books(SQL Server)
+----+---------------------------------+--------------+
| Id | Name                            | LastModified |
+----+---------------------------------+--------------+
| 1  | Modern Operating Systems        | 2019-09-20   |
+----+---------------------------------+--------------+
| 2  | The Art of Computer Programming | 2019-09-20   |
+----+---------------------------------+--------------+
Books(sqlite in mobile App)
+----+---------------------------------+--------------+
| Id | Name                            | LastModified |
+----+---------------------------------+--------------+
| 1  | Modern Operating Systems        | 2019-09-20   |
+----+---------------------------------+--------------+
| 2  | The Art of Computer Programming | 2019-09-20   |
+----+---------------------------------+--------------+
| 9  | Computer Networks               | 2019-09-26   |
+----+---------------------------------+--------------+

Consider that both tables in sqlite and SQL Server contains Auto Increment Primary Key, if use the "Id" as the criteria of merge, it would get a dirty merge.考虑到 sqlite 和 SQL Server 中的两个表都包含 Auto Increment Primary Key,如果使用“Id”作为合并标准,则会得到脏合并。

How should I do the merge in this case?在这种情况下我应该如何进行合并?

Using a composite primary key solves my problem.使用复合主键解决了我的问题。

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

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