gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:55:21 25 4
gpt4 key购买 nike

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

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

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

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

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

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

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

最佳答案

你把事情搞混了。

1)代理键有两种定义

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).



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

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

2)您考虑UUID自然键的逻辑非常滑

引用你的问题:

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.



这不是自然键与代理键的定义或区别特征。自然键具有以下属性(来自 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.



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

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

关于database-design - 使用 uuid 作为主键和/或代理键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com