Id 或 [TableName]Id 作为主键/实体标识符

发布于 2024-07-19 19:05:57 字数 360 浏览 6 评论 0原文

是否首选使用“Id”作为主键的列名或“[TableName]Id”作为命名约定?

表:帐户
主键:Id

-- 与 --

表:帐户
主键:AccountId

在我见过的实现中,它似乎被分割了大约 50% / 50%。 每种方法的优点和缺点是什么?

后续:

在我的数据库中使用一种约定,而在代码中的实体上使用另一种约定是否有意义? 或者我应该保持它们一致? 这在大多数 ORM 中如何发挥最佳作用?

Is it preferred to use "Id" as the column name for a primary key or "[TableName]Id" as a naming convention?

Table: Account
Primary Key: Id

-- versus --

Table: Account
Primary Key: AccountId

It seems to be split about 50% / 50% in the implementations that I've seen. What are the advantages and disadvantages in each approach?

Follow-up:

Does it make sense to use one convention in my database, and another on my entities in code? Or should I keep them consistent? How would does this work best in most ORMs?

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

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

发布评论

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

评论(7

猥琐帝 2024-07-26 19:05:57

TableNameID 清晰可见

  1. 提高 JOIN 可读性
  2. 清晰,例如,当多个 FK“ID”列(PK 与 FK 匹配)时
  3. ID 是保留关键字

TableNameID for clarity

  1. Improve JOIN readability
  2. Clarity, say, when multiple FK "ID" columns (PK matches FK)
  3. ID is a reserved keyword
南城旧梦 2024-07-26 19:05:57

我用的是身份证 如何使用帐户 ID 作为外键来设置用户表? 您将该列命名为 AccountAccountID 还是 AccountID?

但最重要的是无论你选择哪种方式,我的观点都要保持一致。

编辑:我认为在阅读我的评论时,我的论点逻辑是错误的,因为显然你永远不会调用一个字段AccountAccountID。 但乍一看,使用 [TableName]ID 作为主键,[TableName]ID 作为外键对我来说也感觉很奇怪。 似乎您对应该遵循同一组标准的两件事使用了两种不同的规则。 我还认为 Account.ID 和 User.AccountID 更具可读性并且语义更正确。

I use ID. How would you set up User table with an Account ID as a foreign key? Would you name that column AccountAccountID or AccountID?

But the most important part is to be consistent in my opinion, whichever way you choose.

Edit: I think in reading my comment the logic of my argument is off, since obviously you wouldn't ever call a field AccountAccountID. But at first glance using [TableName]ID as the primary key and [TableName]ID as the foreign key as well feels weird to me. It seems like you're using two different rules for two things that should follow the same set of standards. Also I think that Account.ID and User.AccountID is more readable and semantically correct.

等风来 2024-07-26 19:05:57

避免将 ID、状态、描述等常用词作为列名称。

使用 WarehouseID、WarehouseStatus、WarehouseDescription 等名称,当您编写查询、搜索代码、阅读旧代码等时,这些名称将使您的生活更轻松。

Avoid common words like ID, status, description as column names.

use names like WarehouseID, WarehouseStatus, WarehouseDescription, these will make your life easier when you write your queries, search code, read old code, etc.

烟火散人牵绊 2024-07-26 19:05:57

第一种方法更可能是 OOP 命名约定。

第二种方法的优点是可以避免连接查询中出现不明确的列名。 虽然您可以使用别名,但有时这是不可能的,例如在某些 ORM 框架中(例如 EntitySpaces)。

The first method is more likely OOP naming convention.

The second method has the advantage of avoiding ambiguous column names in join queries. Although you can use an alias, sometimes this is not possible, like in some ORM frameworks (EntitySpaces comes to mind).

不可一世的女人 2024-07-26 19:05:57

我发现显式命名(TableId)更好。 首先,所有外键都有一个自然名称([RelatedTable]Id)。 而且我总是最终返回带有两种类型 Id 的连接查询,我被迫正确地为它们起别名,以便客户端可以区分 AccountId 和 ClientId。 使用显式键名称还简化了我的数据访问/orm 层逻辑,因为它不必处理歧义,例如。 帐户类型键始终为“AccountId”,而不是某些查询中的“Id”和其他查询中的“AccountId”。 我的2c。

I found that the explicit naming (TableId) is better. For start all foreign keys have a natural name this way ([RelatedTable]Id). And also I always end up returning joined queries with two types of Id's anyway, where I'm be forced to alias them properly so the client can distinguish between AccountId and ClientId. Using explicit key names also simplifies my data access/orm layer logic as it doesn't have to deal with ambiguity, eg. account type key is always 'AccountId', not 'Id' in some queryies and 'AccountId' in other. My 2c.

风尘浪孓 2024-07-26 19:05:57

我一直使用一种方案,并且发现它非常有用。

表中的主键始终称为“ID”,对于拆分键,我将具有行标识信息的列称为“ID”,否则不调用“ID”列。

所有外键都使用它们引用的表的名称。

CREATE TABLE `Person`  
(
  `ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `FirstName` VARCHAR(255)  NOT NULL,
  `LastName` VARCHAR(255)  NOT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `Tutorial`
(
  `ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `Name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `Class`
(
  `Person` INTEGER UNSIGNED NOT NULL,
  `Tutorial` INTEGER UNSIGNED NOT NULL
  PRIMARY KEY (`Person`, `Tutorial`)
);

Well I use one scheme all the time and find it very useful.

The primary key in a table is always called "ID", with splittet keys I call the column with the row-identing information the "ID" otherwise no "ID" column is called.

All foreign Keys use the name of the table they are referencing.

CREATE TABLE `Person`  
(
  `ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `FirstName` VARCHAR(255)  NOT NULL,
  `LastName` VARCHAR(255)  NOT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `Tutorial`
(
  `ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `Name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `Class`
(
  `Person` INTEGER UNSIGNED NOT NULL,
  `Tutorial` INTEGER UNSIGNED NOT NULL
  PRIMARY KEY (`Person`, `Tutorial`)
);
呆头 2024-07-26 19:05:57

我同意你的看法。 这是一个分裂。 但 Id 本身并没有很好的描述性。 通常我不使用 Id,因为在处理可能存在的 sql 注入风险时,使用 AccountId 安全得多。

I agree with you. It's a split. Id by itself is not very descriptive though. Normally i don't use Id because it's a hell of alot safer to use AccountId when dealing with the possible risk of sql injection.

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