简体   繁体   English

维护移动客户端和服务器之间的参照完整性

[英]Maintaining referential integrity between a mobile client and a server

So I have a relatively simple system. 所以我有一个相对简单的系统。 A mobile client creates records in a sqlite database that I would like to have synced to a remote SQL server (that is shared with other mobile clients) . 移动客户端在sqlite数据库中创建我想同步到远程SQL Server(与其他移动客户端共享)的记录 So when I create a new record in the phone's sqlite table, I then push that change to my remote service through a RESTful API. 因此,当我在电话的sqlite表中创建新记录时,我随后通过RESTful API将更改推送到远程服务。 The problem I'm having, is how do I order the primary keys so that there isn't collisions in the data (ie a record in the phone has the same primary key as a completely different record on the server). 我遇到的问题是如何排序主键,以便数据中没有冲突 (即电话中的记录具有与服务器上完全不同的记录相同的主键)。 What is the usual "best practice for referencing the record on the client, and for referencing the same record on the server? 在客户端上引用记录并在服务器上引用相同记录的最佳做法是什么?

You could use a GUID type of column for the primary key. 您可以将GUID类型的列用作主键。 SQL Server supports the type UNIQUEIDENTIFIER SQLite supports the type GUID as far as i know (otherwise your client app on the phone has to produce a GUID value). 据我所知,SQL Server支持UNIQUEIDENTIFIER类型的SQLite支持GUID类型(否则,电话上的客户端应用程序必须产生GUID值)。 This should guarantee unique values on client and server. 这应该保证客户端和服务器上的唯一值。

GUID is a good choice but not very meaningful. GUID是一个不错的选择,但意义不大。 It would be nice you could send a device ID + an incrementing identity value and/or a time 您可以发送设备ID +递增的标识值和/或时间来很好

That way by looking at the records you could identify what device it came from and the sequence they occured. 这样,通过查看记录,您可以确定设备来自什么设备以及它们发生的顺序。

Depending on your application this could be very useful information. 根据您的应用程序,这可能是非常有用的信息。

Few options, in order of my preference. 按我的喜好顺序很少。 Feel free to clarify in comments if these won't work for your situation: 如果您的情况不适合,请随时在评论中进行澄清:

  • Identify a natural key, or if one doesn't exist create an artificial one that uniquely identifies local data attributes. 识别自然键,或者如果不存在自然键,则创建一个可人工识别本地数据属性的人工键。 Note that an artificial key is different from a surrogate as explained here . 请注意,人工密钥与此处所述的替代密钥不同。 Use this with a unique device ID to create composite primary key 将其与唯一的设备ID结合使用以创建复合主键

  • Use the SQLite ROWID (surrogate key) and a unique device ID as a composite primary key 使用SQLite ROWID (代理键)和唯一的设备ID作为复合主键

  • Use a global unique identifier for each row, beware of potential pitfalls documented here 对每行使用全局唯一标识符,请注意此处记录的潜在陷阱

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

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