表命名困境:单数与复数名称

2021-12-01 00:00:00 sql naming-conventions sql-server

学术界认为表名应该是它们存储属性的实体的单数.

Academia has it that table names should be the singular of the entity that they store attributes of.

我不喜欢任何需要在名称周围使用方括号的 T-SQL,但我已将 Users 表重命名为单数形式,永远判刑那些使用该表的人有时不得不使用方括号.

I dislike any T-SQL that requires square brackets around names, but I have renamed a Users table to the singular, forever sentencing those using the table to sometimes have to use brackets.

我的直觉是保持单数更正确,但我的直觉也是括号表示不受欢迎的内容,例如列名中带有空格等.

My gut feel is that it is more correct to stay with the singular, but my gut feel is also that brackets indicate undesirables like column names with spaces in them etc.

我该留下还是该走?

推荐答案

我有同样的问题,在阅读了这里的所有答案后,我仍然坚持 SINGULAR,原因:

I had same question, and after reading all answers here I definitely stay with SINGULAR, reasons:

原因 1(概念).你可以把装苹果的袋子想象成AppleBag",不管装的是 0、1 还是 100 万个苹果,它总是同一个袋子.表只是容器,表名必须描述它包含的内容,而不是它包含多少数据.此外,复数概念更多是关于一种口语(实际上是为了确定是否有一个或多个).

Reason 1 (Concept). You can think of bag containing apples like "AppleBag", it doesn't matter if contains 0, 1 or a million apples, it is always the same bag. Tables are just that, containers, the table name must describe what it contains, not how much data it contains. Additionally, the plural concept is more about a spoken language one (actually to determine whether there is one or more).

原因 2.(方便).单数名称比复数名称更容易出现.对象可以有不规则的复数形式,也可以根本不是复数形式,但始终是单数形式(很少有像 News 这样的例外).

Reason 2. (Convenience). it is easier come out with singular names, than with plural ones. Objects can have irregular plurals or not plural at all, but will always have a singular one (with few exceptions like News).

  • 客户
  • 订购
  • 用户
  • 状态
  • 新闻

原因 3.(审美和秩序).特别是在 master-detail 场景中,这读取更好,按名称对齐更好,并且具有更多的逻辑顺序(Master 第一,Detail 第二):

Reason 3. (Aesthetic and Order). Specially in master-detail scenarios, this reads better, aligns better by name, and have more logical order (Master first, Detail second):

  • 1.下单
  • 2.订单详情

对比:

  • 1.订单详情
  • 2.订单

原因 4(简单).总而言之,表名称、主键、关系、实体类......最好只知道一个名称(单数)而不是两个(单数类、复数表、单数字段、单复数主细节...)

Reason 4 (Simplicity). Put all together, Table Names, Primary Keys, Relationships, Entity Classes... is better to be aware of only one name (singular) instead of two (singular class, plural table, singular field, singular-plural master-detail...)

  • 客户
  • Customer.CustomerID
  • 客户地址
  • 公共类客户{...}
  • SELECT FROM Customer WHERE CustomerID = 100

一旦您知道您正在处理客户",您就可以确定您将使用同一个词来满足您的所有数据库交互需求.

Once you know you are dealing with "Customer", you can be sure you will use the same word for all of your database interaction needs.

原因 5.(全球化).世界越来越小,你可能有一个不同国籍的团队,不是每个人的母语都是英语.非英语母语程序员更容易想到Repository".比存储库"或状态"而不是状态".使用单一名称可以减少因拼写错误而导致的错误,不必考虑是孩子还是孩子?"从而节省时间,从而提高工作效率.

Reason 5. (Globalization). The world is getting smaller, you may have a team of different nationalities, not everybody has English as a native language. It would be easier for a non-native English language programmer to think of "Repository" than of "Repositories", or "Status" instead of "Statuses". Having singular names can lead to fewer errors caused by typos, save time by not having to think "is it Child or Children?", hence improving productivity.

原因 6.(为什么不?).它甚至可以节省您的书写时间,节省您的磁盘空间,甚至让您的电脑键盘更耐用!

Reason 6. (Why not?). It can even save you writing time, save you disk space, and even make your computer keyboard last longer!

  • SELECT Customer.CustomerName FROM Customer WHERE Customer.CustomerID = 100
  • SELECT Customers.CustomerName FROM Customers WHERE Customers.CustomerID = 103

您已经保存了 3 个字母、3 个字节、3 次额外的键盘敲击 :)

You have saved 3 letters, 3 bytes, 3 extra keyboard hits :)

最后,您可以将那些与保留名称混淆的名称命名为:

And finally, you can name those ones messing up with reserved names like:

  • 用户>LoginUser、AppUser、SystemUser、CMSUser、...

或者使用臭名昭著的方括号 [User]

Or use the infamous square brackets [User]

相关文章