数据库、表和列的命名约定

发布于 2024-08-13 06:53:39 字数 1432 浏览 3 评论 0原文

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

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

发布评论

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

评论(3

享受孤独 2024-08-20 06:53:39

关于命名约定,你永远不会是对的(或错的)。当你从一个雇主换到另一个雇主时,你会在每个工作场所遇到不同的惯例,并且你总是必须适应。您已经说明了自己喜欢什么,因此在处理自己的项目时,只需使用它即可,除非您的系统是构建在其他始终使用另一个约定的东西之上的。那么我想说你最好在该项目中使用该约定。一致性>偏爱。

You're never right (or wrong) about naming conventions. As you go from employer to employer, you'll encounter different conventions at every workplace, and you'll always have to adapt. You've already stated what you prefer, so when working on your own projects, simply use that, except your system is built on top of something else, that consistently uses another convention. Then I'd say you'd be better off using that convention in that project. Consistency > Preference.

不寐倦长更 2024-08-20 06:53:39

如果您不喜欢这个想法,请保持冷静,
但是您是否考虑过列名称独立于表名称

语义

语义是:
“如果两个字段称为 startDate 和 endDate,则它们指定确定当前表所考虑的期间的日期。”

该语义通常跨表,因此具有一致的名称是很好的。

实现问题

在数据库中

也许有人说如果那个公共列名以表名为前缀他们还是明白的。但在几个用例中,我们被这个问题困扰并且更喜欢:

  • 为了有效地使用元请求(例如,创建一个请求来读取名为 startDate 的所有列,或查找使用某些参考数据的所有表),固定名称要容易得多。
  • 如果名称是固定的,存储过程或触发器也可以更容易地重用。

ORM

ORM 确实擅长让您定义一次字段,然后创建自动具有该字段的子类(也使用组合)。数据库没有子类,但是

  • 如果映射到类上的各个表对列使用相同的名称,则一切都很自然。
  • 否则,您必须手动编码(或声明)这样的事实:代码中的 startDate 在数据库中实现为 XXXStartDate 或 XXX_start_date...

需要别名

有些请求是自联接,两次联接同一个表,因此需要在每种情况下都使用表名的别名。

大多数其他手工编码的请求都会连接多个表。此命名策略会增加两列具有相同名称的可能性,因此需要使用别名。这是一个问题吗?我认为这不是因为:

  1. 无论如何,通常建议使用别名,并且被认为是良好实践
  2. 在这两种情况下使用别名可以实现更加一致的编码环境。
  3. 与表名相比,使用别名有一些优点:
    一个。可以允许长且清晰的表名称,包括按“模块”对表进行分组的前缀,因为可以在请求中使用较短的别名。
    b.虽然访问数据库的所有应用程序的所有模块的表名称都是固定的,但应用程序或模块可以在其请求中使用不同的别名,从而允许在请求中提供更多语义(就像命名的选择一样)代码中的变量,具有相同的规则)。

Please keep calm if you don't like this idea,
but did you consider column names independent from table names ?

Semantics

The semantic would be :
"if two fields are called startDate and endDate, then they designates the dates that determine the period considered for the current table."

That semantic usually crosses tables, so having a consistent name is good.

Implementation concerns

In the database

Maybe some people say that they still understand if that common column name is prefixed by the table name. But in several use cases, we got bitten by this and prefer:

  • To you use meta-requests (ex. create a request to read all columns named startDate, or find all tables that use some reference data) efficiently, fixed names are much easier.
  • Stored procedure or triggers also can be reused easier if the names are fixed.

ORM

ORM are really good at letting you define a field once, then create subclasses that will have that field automatically (composition is also used). The database has no subclassing, but

  • If the various tables mapped on the classes use the same name for columns, everything is natural.
  • Otherwise, you have to hand-code (or declare) the fact that the startDate in the code is implemented in the database as XXXStartDate or XXX_start_date...

Requiring Aliases

Some requests are self-joins, joining the same table twice, thus requiring to use aliases for table names in every case.

Most other hand-coded requests join several tables. This naming policy would increase the likelihood that two columns have the same name, so that would require to use aliases. Is that a problem? I think it isn't because:

  1. Using aliases is often recommended anyway, and considered good-practice.
  2. Using aliases in both cases allow for a more consistent coding environment.
  3. Using aliases allow a few advantages over table names:
    a. Can allow long and clear table names, including a prefix to group the tables by "modules", as a shorter alias can be used in requests.
    b. While a table name is fixed for all modules of all applications that access the database, applications or modules can use varying aliases in their requests, allowing to provide more semantic in the requests (just like the choice of naming a variable in the code, with the same rules).
时光与爱终年不遇 2024-08-20 06:53:39

您所显示的两种命名约定都是完全可以接受的。 writingLikeThis 更容易输入,但writing_like_this 更容易阅读。最重要的是一致性。选择一种命名约定并坚持使用。

both naming conventions you've shown are completely acceptable. writingLikeThis is easier to type, but writing_like_this is easier to read. The most important thing is consistency. Pick one naming convention and stick with it.

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