什么时候会使用 GUID 作为主键?

发布于 2024-07-22 07:51:40 字数 296 浏览 5 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮筵 2024-07-29 07:51:41

如果您需要通过复制同步多个数据库,您可以使用 guid 作为键。

使用 guid 的另一个原因是,如果您想在某些远程客户端(例如 winforms 应用程序)上创建行,然后通过 Web 服务等将这些行提交到服务器。

如果您这样做,我强烈建议您确保指定自己的集群基于不唯一的自动递增 int 的索引。 将行插入到聚集索引是 guid 的表中可能会产生相当大的开销。

更新:这是一个如何设置这样的表的示例:

CREATE TABLE [dbo].[myTable](
[intId] [int] IDENTITY(1,1) NOT NULL,
[realGuidId] [uniqueidentifier] NOT NULL,
[someData] [varchar](50) NULL,
    CONSTRAINT [PK_myTable] UNIQUE NONCLUSTERED 
    (
   [realGuidId] ASC
    )
)

CREATE CLUSTERED INDEX [IX_myTable] ON [dbo].[myTable] 
(
[intId] ASC
)

您可以像平常一样插入表中,例如:

INSERT INTO myTable VALUES(NEWID(), 'Some useful data goes here')

更新:我听了一个非常好的 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:

CREATE TABLE [dbo].[myTable](
[intId] [int] IDENTITY(1,1) NOT NULL,
[realGuidId] [uniqueidentifier] NOT NULL,
[someData] [varchar](50) NULL,
    CONSTRAINT [PK_myTable] UNIQUE NONCLUSTERED 
    (
   [realGuidId] ASC
    )
)

CREATE CLUSTERED INDEX [IX_myTable] ON [dbo].[myTable] 
(
[intId] ASC
)

You would insert into the table as normal e.g.:

INSERT INTO myTable VALUES(NEWID(), 'Some useful data goes here')

Update: I listened to a really good dotnetrocks episode that talks about this its worth a listen - Show #447

飘逸的'云 2024-07-29 07:51:41

我使用 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.

一直在等你来 2024-07-29 07:51:41

当数据库不集中或某些收集是远程执行时。

When the database isn't centralized or some of the collection is performed remotely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文