什么时候会使用 GUID 作为主键?
可能的重复:
GUID / UUID 数据库密钥的优点和缺点
在 SQL Server 2005/8 DB 中必须使用 GUID 作为主键的情况。 例如,使用 MS Sync Framework 是否强制执行此操作或数据复制?
Possible Duplicate:
Advantages and disadvantages of GUID / UUID database keys
Are there any circumstances where it is essential to use GUIDs as primary keys in a SQL Server 2005/8 DB. For example, does the use of the MS Sync Framework force this, or data replication?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要通过复制同步多个数据库,您可以使用 guid 作为键。
使用 guid 的另一个原因是,如果您想在某些远程客户端(例如 winforms 应用程序)上创建行,然后通过 Web 服务等将这些行提交到服务器。
如果您这样做,我强烈建议您确保指定自己的集群基于不唯一的自动递增 int 的索引。 将行插入到聚集索引是 guid 的表中可能会产生相当大的开销。
更新:这是一个如何设置这样的表的示例:
您可以像平常一样插入表中,例如:
更新:我听了一个非常好的 dotnetrocks 剧集,其中谈到了这一点,值得一听 - 显示#447
You would use guids as a key if you needed multiple databases synchronising via replication.
Another reason to use guids is if you wanted to create rows on some remote client eg a winforms app and then submit those to the server via web services etc.
If you do this I would strongly suggest that you make sure that you specify your own clustered index based on an auto incrementing int that is not unique. It can be a considerable overhead inserting rows into a table where the clustered index is a guid.
Update: Here is an example of how to set up a table like this:
You would insert into the table as normal e.g.:
Update: I listened to a really good dotnetrocks episode that talks about this its worth a listen - Show #447
我使用 GUID 作为主键,因为当我使用分布式数据库和一个与所有分布式数据库的数据同步的中央数据库构建应用程序时,我不想使用复合主键。 使用 GUID,我确信(几乎*)当我将所有数据库中的数据提取到中央数据库时,不会发生冲突(违反约束)。
* 在两个不同的地方生成相同的 GUID 的可能性极小,但并非不可能。
I am using GUIDs as primary keys, because I don't want to have composite primary keys when I am building applications with distributed databases and one central database that is synchronized with data from all the distributed ones. With GUIDs I am sure (almost*) I will not have a conflict (constraint violation) when I pull data from all the DBs into the central one.
* it is highly unlikely having the same GUID generated in two different places, but not impossible.
当数据库不集中或某些收集是远程执行时。
When the database isn't centralized or some of the collection is performed remotely.