使用 uuid 作为主键和/或代理键?

发布于 2024-10-22 05:24:44 字数 496 浏览 1 评论 0原文

我们需要将 UUID 添加到大多数对象和数据库表中。

您是否会使用 UUID 作为代理键,或者除了序列生成的代理键之外还作为自然键,即使用私有代理键并另外添加列/属性来保存 UUID?

我发现它经常直接用作代理/主键。不知怎的,我不喜欢这个主意。

人们可能将 UUID 视为自然键,因为它应该是一个具有全局含义的唯一标识符,就像任何其他自然键一样,独立于系统的特定实现,即如果您将数据移动到另一个系统, UUID 必须保持不变,而代理键根据定义没有真正且持久的含义。

也许我应该澄清更多:假设我们有一个帐户表。传统上,会有一些内部代理键和一个由帐号组成的自然键(如打印在账户报表等上)。

尽管 UUID 不像帐号那样“可读”,但我认为 UUID 更像是自然密钥,因为它可以起到与帐号相同的作用:以唯一且不变的方式引用特定帐户。 (传统)代理键永远不会出现在系统外部,因为它是完全私有的并且可以随时更改,因此它不能存在任何外部引用。

从这个意义上说,UUID 不是典型的代理键(?)。

We are required to add UUID's to most of our objects and database tables.

Would you use the UUID as surrogate key, or rather as natural key in addition to a sequence generated surrogate key, i.e. use a private surrogate key and in addition add a column/attribute to hold the UUID?

I see that it is often used directly as surrogate / primary key. Somehow I don't like the idea.

One might view a UUID as a natural key, since it should be a unique identifier with a global meaning just like any other natural key, independent of a particular implementation of a system, i.e. if you would ever move your data to another system, the UUID must stay the same, whereas surrogate keys by definition have no real and lasting meaning.

Maybe I should have clarified more: suppose we have an Account table. Traditionally there would be some internal surrogate key and a natural key consisting of the account number (as printed on account statements etc.).

Whereas the UUID is not as "readable" as the account number, I would view a UUID more like the natural key because it could serve the same purpose as the account number: to refer to a particular account in a unique and unchanging manner. The (traditional) surrogate key never appears outside of the system, since it is completely private and could be changed at any time, no external references must ever exist to it.

In that sense a UUID is not a typical surrogate key (?).

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

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

发布评论

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

评论(1

闻呓 2024-10-29 05:24:44

你把事情搞混了一点。

1)代理键有两种定义

代理人 (1)

该定义基于 Hall、Owlett 和 Todd (1976) 给出的定义。
这里代理代表一个实体
在外面的世界。代理是
由系统内部生成,但是
尽管如此,对用户还是可见的,或者
应用。

代理人 (2)

该定义基于 Wieringa 和 De Jonge (1991) 给出的定义。
这里代理代表一个对象
在数据库本身。代理人
由系统内部生成
并且对用户不可见或
应用程序。

代理项 (1) 定义定义
它在数据模型中的用法
比存储模型并用于
本文。参见日期(1998)。

(来自 wiki 上关于 代理 键的条目;带着一点怀疑的态度阅读这篇文章 - 例如引用 ' 代理键比复合键的连接成本更低(需要比较的列更少)'表面上看起来似乎很合理,但自然复合键将创建自然排序和隔离的索引,从而实现非常高效的扫描当浏览或分析数据时,也由于返回包含多行的结果集的相同逻辑连接实际上可以执行得更好)

无论如何,当从数据模型的角度考虑代理键时,您不应该考虑您所说的“传统”定义。

2)您考虑 UUID 自然键的逻辑非常狡猾,

引用您的问题:

我认为 UUID 更像是
自然键,因为它可以服务
与账号相同的目的:
引用某个特定帐户
独特且不变的方式。

这不是自然键与代理键的定义或区别特征。自然键具有以下属性(来自 wiki):

自然键是候选键
具有逻辑关系
该行中的属性。一个自然的
密钥有时称为域密钥。

自然键的主要优点
通过代理键,它没有
这样的逻辑关系,就是
已经存在;没有必要
添加一个新的人工列
架构。使用自然键(当一个
可以被识别)也简化了
数据质量:确保
一个键只能是一行;这
“真相的一个版本”可以是
已验证,因为自然键是
基于现实世界的观察。

通常,UUID 和同一行的属性之间没有逻辑关系。但是,如果 UUID 是由外部系统分配的,并且您已经需要将它们存储为属性,那么您就拥有该逻辑(类似于您可以将序列号或社会保险号视为自然键)。

仅在这个意义上,UUID 可能不再是代理键,但您仍然可能(并且可能会有)同一行的另一个候选键具有更强大和更丰富的逻辑。

You are mixing things up a bit.

1) There are two definitions of surrogate keys

Surrogate (1)

This definition is based on that given by Hall, Owlett and Todd (1976).
Here a surrogate represents an entity
in the outside world. The surrogate is
internally generated by the system but
is nevertheless visible to the user or
application.

Surrogate (2)

This definition is based on that given by Wieringa and De Jonge (1991).
Here a surrogate represents an object
in the database itself. The surrogate
is internally generated by the system
and is invisible to the user or
application.

The surrogate (1) definition defines
its usage in the data model rather
than the storage model and is used in
this article. See Date (1998).

(from wiki's entry on surrogate keys; read the article with a bit of scepticism - for example quote 'Surrogate keys are less expensive to join (fewer columns to compare) than compound keys' might seem reasonable on the surface, but natural compound keys will create indexes that are naturally ordered and segregated, allowing for very efficient scans when browsing or analyzing data, also due to the same logic joins that return resultsets containing several rows can actually perform much better)

Anyway, when considering surrogate keys from the perspective of the data model, you should not consider what you call a 'traditional' definition.

2) Your logic for considering UUIDs natural keys is very slippery

quoting from your question:

I would view a UUID more like the
natural key because it could serve the
same purpose as the account number: to
refer to a particular account in a
unique and unchanging manner.

This is not a defining nor distinguishing characteristic of natural keys vs surrogate keys. Natural keys have following properties (from wiki):

A natural key is a candidate key that
has a logical relationship to the
attributes within that row. A natural
key is sometimes called a domain key.

The main advantage of a natural key
over a surrogate key, which has no
such logical relationship, is that it
already exists; there is no need to
add a new, artificial column to the
schema. Using a natural key (when one
can be identified) also simplifies
data quality: It ensures that there
can only be one row for a key; this
"one version of the truth" can be
verified, because the natural key is
based on a real-world observation.

Normally there is no logical relationship between UUID and the attributes of the same row. However, if UUIDs are assigned by an external system and if you already have a requirement to store them as an attribute then you have that logic (similarly like you could consider a serial number or social security number a natural key).

Only in this sense UUID might stop being surogate key and yet still you might have (and probably will have) stronger and richer logic for another candidate key for the same row.

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