没有主键或代理键?

发布于 2024-12-18 01:44:13 字数 294 浏览 0 评论 0原文

我将创建一个表,其中包含客户 ID (GUID)、数据点(字符串)和输入时间。但是,我不知道如何最好地键入它。我不能保证三元组 ID/字符串/时间是唯一的(该表将包含用户输入,并且我们预见某些用户将尝试编写表单提交脚本,因此时间可能没有足够的分辨率)。

我们会经常在此表中进行查找,因此查询速度很重要。另一方面,插入件也不能完全损坏。

因此,我看到两个选择:要么不使用主键,要么定义代理自动增量键。考虑到上述要求,什么是最好的?我可以对三个数据列使用代理键和非唯一索引吗?

我们使用的是 SQL Server 2008 R2。

I'm about to create a table which will hold a customer ID (GUID), a data point (string) and the time of entry. However, I can't figure out how to best key it. I cannot guarantee that the 3-tuple Id/string/time is unique (the table will contain user input, and we foresee that some users will try to script form submissions, therefore the time might not have sufficient resolution).

We will do lookups in this table quite often, so query speed is important. On the other hand, inserts can't be totally crippled either.

So, I see two choices: Either go without a primary key, or define a surrogate auto-increment key. What would be the best given the above requirements? Could I use a surrogate key and a non-unique index for the three data columns?

We're using SQL Server 2008 R2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小兔几 2024-12-25 01:44:13

除非您有充分的理由这样做,否则默认数据库设计使用代理主键,并根据需要设置 IDENTITY 属性,使用smallint/int/bigint,以便生成自动创建的主键值。如果您稍后决定要使用表中的一列或多列强制使用自然键,则可以通过创建 UNIQUE CONSTRAINT 来实现。

虽然表上可以没有唯一约束、可以有一个或多个唯一约束,但只能有一个主键,因此将其用作代理主键,这样做可以避免许多其他问题。

Unless you have good reason to do otherwise, default your database design to use a surrogate primary key using a smallint/int/bigint as appropriate with the IDENTITY property set so that it will generate automatically created primary key values. If you decide later on that you want to enforce a natural key using one or more columns in the table, you can do so by creating a UNIQUE CONSTRAINT.

Although you can have none, one or many unique constraints on a table, you can only have one primary key, so use it as a surrogate primary key and avoid a lot of other problems in so doing.

別甾虛僞 2024-12-25 01:44:13

那么包含这样的行的表意味着什么呢?

customer_id                           data_point     time_entered
--
6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567

具有像 this 这样的行的表意味着什么?

id  customer_id                           data_point     time_entered
--
1   6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
2   6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
3   6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567

如果在将身份证号码挂在桌子上之前你对第一个问题没有一个好的答案,那么之后你也不会得到一个好的答案。

So what does a table that has rows like this mean?

customer_id                           data_point     time_entered
--
6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567

And what does a table that has rows like this mean?

id  customer_id                           data_point     time_entered
--
1   6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
2   6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567
3   6F9619FF-8B86-D011-B42D-00C04FC964FF  some data      2011-11-23 10:02:12.34567

If you don't have a good answer to the first question before you hang an id number on that table, you won't have a good answer afterwards, either.

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