参考表是否应该包含数字 PK 标识列值 0?
我们有一个包含有效货币代码的表。例如,我们选择使用数值作为主键,而不是 3 字符 ISO 货币代码。
普遍的共识是,此 CurrencyId
列应包含以零开头的值。由于美元是我们的主要货币,因此它以 0 的值占据第一个位置。
我的想法是,标识列不应该从零开始,唯一的原因是某些语言将数字初始化为零,因此货币代码可能会无意中设置为 USD
,而实际上从未分配过它。
我浑身湿透了吗?我更愿意将 1 的 CurrencyId
分配给 USD
。
We have a table that contains the valid currency codes. We are choosing to use a numeric value as the primary key rather than a 3 char ISO Currency code, for example.
General consensus has concluded that this CurrencyId
column should contain values that begin with zero. Since the US dollar is the primary currency for us, it claimed the first position with a value of 0.
My thought is that identity columns should not start at zero for the sole reason that some languages initialize numerics to zero and as a result the currency code may be unintentionally set to USD
when really it was never assigned.
Am I all wet? I would prefer to assign a CurrencyId
of 1 to USD
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际的 ID 应该不重要。让它从 0(或 1)开始是完全任意的,因为该数字对用户没有任何意义——它仅被系统用作参考。无论是从 0、1 还是 4,536,901 开始,都不是关键的设计决策。
我认为你提出的问题100%有效;从 0 开始可能会导致副作用,其中一些最终可能会成为微妙且难以追踪的错误。避免这种远远胜过任何人对从零开始编号的任意/审美愿望的反对。
The actual Id should not matter. Having it start at 0 (or 1 for that matter) is completely arbitrary, as that number has no meaning to the user--it is only used by the system as a reference. Whether it starts at 0 or 1 or 4,536,901 is not a critical design decision.
I think the issue that you raise is 100% valid; starting at 0 can cause side effects, some of which may end up as bugs that are subtle and difficult to track down. Avoiding this far outweighs anyone's objection to an abitrary/aethstetic desire to start the numbering at zero.