SQL - ID 列的命名

发布于 2024-08-17 04:55:27 字数 327 浏览 3 评论 0原文

我一直想知道 SQL 中这些 ID 命名风格的优点和缺点是什么:

CREATE TABLE cache (
id INT AUTO_INCREMENT,
PRIMARY KEY(id)
);

CREATE TABLE cache (
cid INT AUTO_INCREMENT,
PRIMARY KEY(id)
);

CREATE TABLE cache (
cache_id INT AUTO_INCREMENT,
PRIMARY KEY(id)
);

为什么有些开发人员在每个表中使用“id”,有些开发人员在其前面加上表名的一个字母或整个表名加上一个字母下划线?

I've always wondered what are the pros and the cons of these ID naming styles in SQL:

CREATE TABLE cache (
id INT AUTO_INCREMENT,
PRIMARY KEY(id)
);

CREATE TABLE cache (
cid INT AUTO_INCREMENT,
PRIMARY KEY(id)
);

CREATE TABLE cache (
cache_id INT AUTO_INCREMENT,
PRIMARY KEY(id)
);

Why some developers use "id" in each table, some prefix it with one letter of the table name or with the entire table name along with one underscore?

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

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

发布评论

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

评论(8

2024-08-24 04:55:27

主观的,但我喜欢使用命名的 id(例如 customer_id、item_id 等)。

我的推理是,如果你一致地命名你的外键,它会使连接更容易理解 - 它总是 a.customer_id = b.customer_id。否则,对于使用大量连接的复杂查询,您将拥有大量“id”列,并且不能立即明显看出什么与什么对应。

ETA:

此外,如果您使用 MySQL,则可以使用更简单的联接语法,例如:

FROM customers INNER JOIN orders USING customer_id

Subjective, but I like to use named ids (e.g. customer_id, item_id, etc.)

My reasoning is that if you name your foreign keys consistently it makes joins easier to understand - it's always a.customer_id = b.customer_id. Otherwise, with complicated queries that use lots of joins, you have a sea of "id" columns and it's not immediately obvious what goes with what.

ETA:

Also, if you are using MySQL, you can use the simpler join syntax, e.g.:

FROM customers INNER JOIN orders USING customer_id
奢华的一滴泪 2024-08-24 04:55:27

这都是个人喜好。我个人使用 Id 只是因为我将每个表视为自己的实体...然后,当我使用键引用时,它会根据表的名称变成 CustomerId 或 OrderId。

It's all a personal preference. I personally use Id simply because I think of each table as its own entity...then when I reference with a key it becomes CustomerId or OrderId depending on the name of the table.

蓝颜夕 2024-08-24 04:55:27

仅使用“id”的优点是您有一个统一的字段,并且很容易记住和输入。

在表名前加上 id 前缀的优点是,在频繁处理多个表时可以更轻松地使用。

cid 似乎是三种选择中最差的一种,没有其他两种选择的好处。

The pros of just using 'id' is that you have a uniform field, and it's easy to remember and type.

The pros of prefixing id with the table name is that it can be easier to work with when working with multiple tables frequently.

cid seems like the worst of three options, with none of the benefits of the other two.

┾廆蒐ゝ 2024-08-24 04:55:27

<块引用>

原生 SQL:最好使用比 id 更具体的内容,这样编写 sql 的人就不必总是以表别名作为前缀。通过消除对别名的需要,您可以显着缩小 SQL 的大小,同时消除大量错误。

即使使用表名,前缀也不会比更具体的列名多多少个字符:

customer.id
customer_id

另一方面,由于外键列引用表,为什么不直接使用表名作为外键呢?

table order (
     id        SERIAL PRIMARY KEY,
     customer  INTEGER NOT NULL REFERENCES (customer)
...

然后我们有:

FROM customer INNER JOIN order ON customer.id = order.customer

Native SQL: its best to use something more specific than id so that the humans writing the sql don't always have to prefix with a table alias. By eliminating the need for aliases you can significantly shrink the size of the SQL and eliminate a lot of errors at the same time.

Prefixing, even with a table name, is no more characters than a more specific column name:

customer.id
customer_id

On different note, since the foreign key column references a table, why not just use the name of the table as the foreign key?

table order (
     id        SERIAL PRIMARY KEY,
     customer  INTEGER NOT NULL REFERENCES (customer)
...

Then we have:

FROM customer INNER JOIN order ON customer.id = order.customer
猥︴琐丶欲为 2024-08-24 04:55:27

第一个决定是是否使用“id”,它是由处理 SQL 的工具驱动的:

  • ORM:有些人更喜欢“id”
  • 原生 SQL:最好使用比 id 更具体的东西,这样编写 sql 的人就不会这样做始终必须以表别名作为前缀。通过消除对别名的需求,您可以显着缩小 SQL 的大小,同时消除大量错误。

第二个决定是,如果您使用需要“id”的 ORM,则:

  • 您可以对所有列名称使用 ORM 约束
  • ,或者您可以为人类命名列,并创建一个单独的视图,将列重命名为ORM 想要的。这是一项工作,但如果您不仅仅有查看表的 ORM(报告工具、其他 ORM 等),那么这可能是值得的。

或者,如果您不使用“id”,第二个决定是 - 如何在关系数据库中构建名称:

  • 您通常没有大小写可使用 - 所以驼峰式等不起作用
  • 您想避免命名冲突
  • 您希望名称在不知道表名称的情况下直观一点
  • 您希望名称格式一致
  • 因此,cid 不太好。更可取的是cache_id。

The first decision is 'id' or not, and it is driven by tool that's handling the SQL:

  • ORM: some prefer 'id'
  • Native SQL: its best to use something more specific than id so that the humans writing the sql don't always have to prefix with a table alias. By eliminating the need for aliases you can significantly shrink the size of the SQL and eliminate a lot of errors at the same time.

The second decision, if you're using an ORM that demands 'id', is:

  • You can just go with ORM constraints on all column names
  • Or you can name the columns for humans, and create a separate view that renames the columns to what the ORM wants. This is a bit of work, but if you have more than just the ORM looking at the tables (reporting tools, other ORMs, etc) it may be worthwhile.

Or the second decision, if you're not using 'id', is - how to build names in a relational databases:

  • You typically don't have case to use - so camelcase, etc don't work
  • You want to avoid naming collisions
  • You want names to be a little intuitive without knowing the table name
  • You want names to be consistently formatted
  • So, cid isn't very good. cache_id is preferable.
相守太难 2024-08-24 04:55:27

当您将每个表的主键命名为“id”时,某些 ORM 会“更好”地工作。

Some ORMs work "better" when you name each table's primary key as "id".

清醇 2024-08-24 04:55:27

正如其他一些人已经指出的那样:这确实是个人喜好。

我或多或少坚持第三种方法(表 foo 将获得 id 列“foo_id”),但不能真正告诉你为什么;-)

第一种方法的优点是你可以重命名你的表,而不必重命名你的 id 列以反映变化。但这并不是将其定为学说的理由。

As a few other have already pointed out: It's really a personal preference.

I more or less stick to the third approach (table foo will get the id column "foo_id") but cannot really tell you why ;-)

An advantage of the first approch ist that you can rename your table without having to rename your id column to reflect the change. But that's hardly a reason to make it a doctrine.

影子的影子 2024-08-24 04:55:27

便利:
第一个区别是数据库设计者的思维方式和代码开发人员(程序员)的思维方式之间的区别。
程序员认为 id 是一个对象(这里是缓存对象)的属性,因此使用它是完全荒谬的。

cache.cache_id

另一方面,对于数据库开发人员来说,使用 cache_id 是有意义的,例如当需要连接表并使用别名时表。比较这两个代码:

select c.cache_id, c.name, ci.created_at as cache_date
from cache c 
join cahce_item ci on ci.cache_id = c.cache_id

随着

select c.id as cahce_id, c.name, ci.created_at as cache_date
from cache c 
left join cahce_item ci on c.id = ci.cache_id

select 语句中表数量的增加,跟踪查询中的别名和 id 变得越来越困难。

概念:
第二个区别来自对象和表之间的关系。对象是类的实例,而表是行的集合。尽管模型类的实例可以表示表的一行,但有些人错误地认为类是表的表示,这是完全错误的。这正是为什么大多数 ORM 系统都有配置,开发人员可以在对象和表之间进行映射(例如表的复数名称和对象的单数名称或 pk 列的 tablename_id 和对象的 id)。根据我的经验,当数据库由 ORM 生成并且由程序员负责时,它总是id

因此,作为一个数据库设计者,如果你有命名的权限,我建议使用 tablename_id(cache_id) 并告诉程序员使用 ORM 配置来处理命名。这样,数据库开发人员和程序员都会感到高兴:

select cahce_id from caches

对于数据库开发人员和

cache.id

程序员来说。

Convenience:
The first difference is between how a database designer thinks and how a code developer (programmer) thinks.
A pargrammer thinks that id is a property of an object(here cache object) so it is completely rediculous that one uses

cache.cache_id

On the other hand for a database developer it makes sense to use cache_id for example when one needs to join tables and uses aliases for tables. Complare these two codes:

select c.cache_id, c.name, ci.created_at as cache_date
from cache c 
join cahce_item ci on ci.cache_id = c.cache_id

and

select c.id as cahce_id, c.name, ci.created_at as cache_date
from cache c 
left join cahce_item ci on c.id = ci.cache_id

as the number of tables grows in the select statement, its getting more and more harder to keep track of the aliases and ids in the query.

Concept:
Second difference comes from the realtion between object and table. An object is an instance of a class but a table is a collection of rows. Though an instance of a model class can represent a row of a table, by mistake some people consider a class as a representation of a table which is a completely wrong. This is exactly why most of the ORM systems have configurations and developers can map between objects and tables ( e.g. plural name for tables and singular name for objects or tablename_id for pk column and id for object). In my experience, its always id when database is generated by ORM and a programmer is in charge.

Therefore, as a databse designer if you have the authority over naming, I suggest use the tablename_id( cache_id) and tell the programmer to handle the naming however they want using the ORM configuration. This way both database developer and programmer are happy:

select cahce_id from caches

for the database developer and

cache.id

for the programmer.

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