哪种方法更好?每个数据库表的唯一表ID或全局/应用程序范围中的唯一表ID?

时间:2016-05-13 05:52:24

标签: database database-design

您更喜欢哪一个设计数据库?为什么?

  1. 在每个表范围内维护生成的唯一ID。
  2. 在全局/应用程序范围内维护生成的唯一ID。

1 个答案:

答案 0 :(得分:0)

The only advantage I see in per-table surrogate key values is possibly smaller representations saving a little bit of memory. However, the possibility of overlap makes it difficult to create supertypes without redesigning existing tables.

Consider a database that records companies and persons separately. We might later want to associate addresses with both sets. If their key values were guaranteed distinct (and of the same data type), it would be a simple matter to consolidate both sets and relate them to their addresses. If, however, their keys overlapped, we would have to choose one of the following less convenient options: assign another unique identifier to each company and person (in addition to or in place of the existing conflicting keys) or address them via a composite key using a combination of their existing keys and a type or table indicator.

I certainly don't think every table should automatically have a surrogate key, but where surrogate keys are applicable, making them globally unique prevents some difficulties later.