为什么在列名前面加上表名是一种惯例?

发布于 2024-12-10 10:42:32 字数 160 浏览 0 评论 0原文

我在很多数据库中都看到过这种约定,但它是多余的。一些答案说这是为了减少复杂连接期间的混乱,但这并不是一个充分的理由。如果要进行复杂的连接,请创建别名。

连接真的代表了这样一个常见的任务,以至于我们应该使选择、插入和更新等标准任务变得多余吗?

为什么在列名前面加上表名是一种惯例?

I have seen this convention in many databases, but it is redundant. A few answers say it is to reduce confusion during complex joins, but this isn't a sufficient reason. If you are making complex joins, make aliases.

Do joins really represent such a common task that we should make standard tasks like selects, inserts, and updates redundant?

Why is prefixing column names with the table name a convention?

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

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

发布评论

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

评论(8

千纸鹤 2024-12-17 10:42:32

我认为实际上并不存在用表名作为列名前缀的约定。

正如 Philippe Grondier 详细介绍的那样,数据建模的“正确”方法是首先创建数据元素名称的字典。遵循国际标准 ISO 11179 指南:

[Object] [Qualifier] Property RepresentationTerm

您最终会得到完全合格的数据元素。这里的限定符元素 ObjectQualifier 以及有时的 Property 是您认为的“前缀”的组合。

在 SQL 中实现数据模型时,表名可以提供上下文并引导设计者从列名中删除限定术语。我认为这是您更喜欢的约定。**

换句话说,在您质疑的约定中,并不是表名已作为列名的前缀,而是保留了限定术语。


** 无论您的约定还是任何其他约定是否良好都是主观的,Stackoverflow 不是进行此类讨论的场所。不过,我要顺便提一下,保留限定条件确实有实际好处(并且理论上合理),例如,考虑到 SQL 的NATURAL JOIN 适合在整个模式中一致命名的列。

I don't think there is actually a convention of prefixing column names with the table name.

As Philippe Grondier details, the 'proper' approach to data modelling is to first create a dictionary of data element names. Following the international standard ISO 11179 guidelines:

[Object] [Qualifier] Property RepresentationTerm

you end up with data elements that are fully qualified. Here the qualifier elements Object, Qualifier and sometimes Property are in combination what you consider to be the 'prefix'.

On implementation of the data model in SQL, the table name can provide the context and leads the designer to drop the qualifying terms from the column name. I think this is convention you prefer.**

In other words, in the convention you are questioning it is not that the table name has been prefixed to the column name, rather it is that the qualifying terms have been retained.


** whether or not yours or any other is a good convention is subjective and Stackoverflow is not the place for such discussion. However, I will mention in passing that retaining qualification terms does have a practical benefits (as well as being theoretically sound) e.g. consider that SQL's NATURAL JOIN lends itself to columns that are named consistently throughout the schema.

古镇旧梦 2024-12-17 10:42:32

确实,这种“开发的列名称”方法广泛用于列命名,例如,Tbl_Person 将具有 id_Person 主键列和 personName 文本列。

虽然一开始写“开发”列名(如“id_Person”、“personName”、“personAdress”等)可能看起来相当痛苦,但当您必须在多个表上编写 SELECT 时,一切都会变得更加清晰,这是每次都会发生的事情您打开表格或报告。

这种“开发的列名称”方法还有一个理论/历史维度。第一个关系数据库理论和方法(如 MERISE)提出,作为第一步,构建所谓的“数据字典”,即应用程序\数据库要操作的所有数据的列表。

甚至在提出任何“实体-关系”模型之前就必须建立该字典。 数据名称/描述必须得到充分开发,这是为了避免“相似”数据条目之间的混淆,例如“companyName”和“personName”。

因此,“开发的列名称”约定反映了这样一个事实:在数据级别,相似的列(例如 Company.name 和 Person.name 列)< strong>并不像看起来那样等效。虽然它们看上去都是为了持有名字而来,但其中一个是为了持有公司名称,而另一个则是为了持有个人名字!

这种约定可以被认为是反映数据库每个列的确切含义,或者反映数据字典中每个条目的确切含义的一种方式。

It is true that such "developped column names" methods are widely used for column naming where, for example, Tbl_Person will have an id_Person primary key column, and a personName text column.

Though it might seem at first quite painfull to write 'developped' column names like "id_Person", "personName", "personAdress", etc, everything gets clearer when you have to write SELECT's on multiple tables, which is something that happens each time you open a form or a report.

There is also a theoretical/historical dimension to this "developped column names" method. First relational databases theories and methods (like MERISE) were proposing, as a first step, to build the so-called "data dictionary", ie the list of all data to be manipulated by the app\database.

This dictionary has to be established even before any "Entity-Relation" model is proposed. data names/descriptions have then to be fully developped, this to avoid confusion between 'similar' data entries, like, for example "companyName" and "personName".

Thus, the "developped column names" convention reflects the fact that, at the data level, similar columns (such as a Company.name and a Person.name columns) are not as equivalent as they seem to be. Though they both look like being here to hold a name, one of them is made to hold a company name, while the other is made to hold a person's name!

This convention can then be considered as a way to reflect the exact meaning of each of the database's column, or to reflect the exact meaning of each entry in the data dictionary.

海夕 2024-12-17 10:42:32

我从未见过带前缀的完整表名,但通常至少有一个缩写。你是完全正确的,这是为了简化连接等。例如,始终写 ur_id 比有时写 id 和其他时候写 userrights.id 更容易。一次需要访问多个表的情况并不少见。

I've never seen the full table name prefixed, but usually at least an abbreviation. And you're exactly right, it's for simplicity in joins and the like. It's easier to write ur_id all the time than it is to write id sometimes and userrights.id other times, for example. It's not that uncommon to need to access more than one table at a time.

身边 2024-12-17 10:42:32

连接是选择的一部分,因此比较不成立。

除此之外,我认为您不应该在字段前加上表名作为前缀,主键除外。我喜欢给每个表一个代理键,我宁愿以表的名字命名。因此表“Orders”将获得“OrderId”PK。订单行将有一个外键 OrderId 来指向订单。这样,各个表中的字段名称都是相同的,您可以通过名称来判断它显示了哪些数据。您可以在所有表​​中将该字段命名为“Id”,但您必须读取别名才能了解您指的是哪个 ID。我写的一些查询超过 400 行。您不想单独依赖表别名。字段名本身的一些上下文确实有帮助。

Join is part of a select, so that comparison doesn't hold.

That aside, I don't think you should prefix the field with the table name, except for primary keys. I like to give every table a surrogate key, which I rather name after the table. So the table 'Orders' will get an 'OrderId' PK. An order line will have a foreign key OrderId to point to the order. That way, the field names are the same across tables, and you can tell by the name, which data it presents. You could name the field just 'Id' in all tables, but you do have to read the alias to see which ID you mean. Some queries I wrote are over 400 lines. You don't want to rely on table aliases alone. A little context in the fieldname itself does help.

岁月蹉跎了容颜 2024-12-17 10:42:32

这不是一个惯例;而是一个惯例。有些人这样做,有些人不这样做。我更经常看到 ID 列以表名为前缀,但没有其他列。一些(全部?)数据库还允许在查询中使用表名作为前缀,但这既不是必需的,也不是实际列名的一部分。

It's not a convention; some people do it, some people don't. More often I see an ID column prefixed with the table name, but no other columns. Some (all?) DBs also allow prefixing with the table name in queries, but it's neither required, nor part of the actual column name.

伴梦长久 2024-12-17 10:42:32

我不喜欢这样的命名约定。它鼓励懒惰,特别是在查询中使用不合格的引用。为查询中的每个表使用别名,并使用适当的别名限定每个列引用。

我唯一喜欢的命名约定与主键/外键有关:

  • 我喜欢将主键命名为一些巧妙的名称,例如 id
  • 我喜欢用包含主键的表的名称作为外键列名称的名称前缀。

恕我直言,它使 SQL 变得更加清晰。一个例子:

   create table foo
   (
     id int not null primary key ,
     ...
   )

   create table bar
   (
     id int not null primary key ,
     foo_id int not null foreign key references foo (id) ,
     ...
   )

   select *
   from foo foo
   join bar bar on bar.foo_id = foo.id

当然,当你使用复合键时,这个方案就会失败。但我喜欢它。 YMMV。

I dislike such naming conventions. It encourages sloth, specifically the use of unqualified references in queries. Use an alias for each table in your query and qualify each column reference with the appropriate alias.

The only such naming convention I like has to do with primary/foreign keys:

  • I like to name primary keys something clever, like id.
  • I like to name prefix the names of foreign key columns with the name of the table containing the primary key.

It makes for much more legible SQL, IMHO. An example:

   create table foo
   (
     id int not null primary key ,
     ...
   )

   create table bar
   (
     id int not null primary key ,
     foo_id int not null foreign key references foo (id) ,
     ...
   )

   select *
   from foo foo
   join bar bar on bar.foo_id = foo.id

This scheme falls down, of course, when you get to compound keys. But I like it. YMMV.

溇涏 2024-12-17 10:42:32

除了其他人所说的之外,在识别关系(也称为识别外键)的情况下,它还使事情变得更简单。

标识关系将父级主键“迁移”到子级主键的一部分。前缀可确保不会发生冲突,并且即使存在多个级别的标识关系,您也无需重命名迁移的字段。例如:

PARENT:
    PARENT_NAME      PK

CHILD:
    PARENT_NAME      PK, FK referencing PARENT
    CHILD_NAME       PK

GRANDCHILD:
    PARENT_NAME      PK, FK referencing CHILD
    CHILD_NAME       PK, FK referencing CHILD
    GRANDCHILD_NAME  PK

在整个数据模型中保持相同的名称可以避免对字段的含义和来源产生任何混淆。

另一方面,前缀可能会影响可读性,因此我通常会采取妥协:为主键字段添加前缀,但其他字段不添加前缀。

In addition to what others said, it is also makes things simpler in the presence of identifying relationships (a.k.a. identifying FOREIGN KEYs).

An identifying relationship "migrates" the parent's primary key into a part of child's primary key. Prefix ensures there will be no collision and you won't need to rename the migrated fields, even when there are multiple levels of identifying relationships. For example:

PARENT:
    PARENT_NAME      PK

CHILD:
    PARENT_NAME      PK, FK referencing PARENT
    CHILD_NAME       PK

GRANDCHILD:
    PARENT_NAME      PK, FK referencing CHILD
    CHILD_NAME       PK, FK referencing CHILD
    GRANDCHILD_NAME  PK

Keeping the same name throughout the whole data model avoids any confusion as to what the field means and where it came from.

On the other hand, prefixing can take a toll on readability, so I usually take a compromise: prefix primary key fields but leave other fields unprefixed.

逆蝶 2024-12-17 10:42:32

特殊情况

正如其他人指出的那样,没有强制理由这样做。通过完整的 schema.table.col 约定可以轻松消除歧义。

不过,有一个实际原因,即当您的物理环境使用 MySQL 方言作为 DML 时。这种语言类型方便地支持 JOIN 中的 USING
clause 这使得更容易编写和阅读语句。

进行比较

将关系 t1 与 PK t1_IDt2 与 PK t2_ID 和 FK t1_ID 代码>

SELECT t1.a, t2.b
FROM t1
INNER JOIN t2 USING(t1_ID)


关系 t1 带有 PK IDt2 带有 PK ID 和 FK t1_ID >

SELECT t1.a, t2.b
FROM t1
INNER JOIN t2 on t1.ID = t2.t1_ID

看起来差别很小,但表名较长,版本一提供了稍微更好的可读性,因此不易出错的代码。

A special case

As others have pointed out there is no impirative reason to do so. Ambiguity can easily be fought with a complete schema.table.col convention.

There is one practicle reason though and that when your physical environment uses a MySQL-dialect as DML. This language type conveniently supports a USING in a JOIN
clause which makes for easier writing and reading statements.

Compare

With a relation t1 with PK t1_ID and t2 with a PK t2_ID and FK t1_ID

SELECT t1.a, t2.b
FROM t1
INNER JOIN t2 USING(t1_ID)

and
With a relation t1 with PK ID and t2 with a PK ID and FK t1_ID

SELECT t1.a, t2.b
FROM t1
INNER JOIN t2 on t1.ID = t2.t1_ID

Which appears small a difference but with longer table names version one offers slightly better readable and hence less error prone code.

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