SQL - ID 列的命名
我一直想知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
主观的,但我喜欢使用命名的 id(例如 customer_id、item_id 等)。
我的推理是,如果你一致地命名你的外键,它会使连接更容易理解 - 它总是 a.customer_id = b.customer_id。否则,对于使用大量连接的复杂查询,您将拥有大量“id”列,并且不能立即明显看出什么与什么对应。
ETA:
此外,如果您使用 MySQL,则可以使用更简单的联接语法,例如:
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.:
这都是个人喜好。我个人使用
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.仅使用“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.
即使使用表名,前缀也不会比更具体的列名多多少个字符:
另一方面,由于外键列引用表,为什么不直接使用表名作为外键呢?
然后我们有:
Prefixing, even with a table name, is no more characters than a more specific column name:
On different note, since the foreign key column references a table, why not just use the name of the table as the foreign key?
Then we have:
第一个决定是是否使用“id”,它是由处理 SQL 的工具驱动的:
第二个决定是,如果您使用需要“id”的 ORM,则:
或者,如果您不使用“id”,第二个决定是 - 如何在关系数据库中构建名称:
The first decision is 'id' or not, and it is driven by tool that's handling the SQL:
The second decision, if you're using an ORM that demands 'id', is:
Or the second decision, if you're not using 'id', is - how to build names in a relational databases:
当您将每个表的主键命名为“id”时,某些 ORM 会“更好”地工作。
Some ORMs work "better" when you name each table's primary key as "id".
正如其他一些人已经指出的那样:这确实是个人喜好。
我或多或少坚持第三种方法(表 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.
便利:
第一个区别是数据库设计者的思维方式和代码开发人员(程序员)的思维方式之间的区别。
程序员认为 id 是一个对象(这里是缓存对象)的属性,因此使用它是完全荒谬的。
另一方面,对于数据库开发人员来说,使用 cache_id 是有意义的,例如当需要连接表并使用别名时表。比较这两个代码:
随着
select 语句中表数量的增加,跟踪查询中的别名和 id 变得越来越困难。
概念:
第二个区别来自对象和表之间的关系。对象是类的实例,而表是行的集合。尽管模型类的实例可以表示表的一行,但有些人错误地认为类是表的表示,这是完全错误的。这正是为什么大多数 ORM 系统都有配置,开发人员可以在对象和表之间进行映射(例如表的复数名称和对象的单数名称或 pk 列的 tablename_id 和对象的 id)。根据我的经验,当数据库由 ORM 生成并且由程序员负责时,它总是id。
因此,作为一个数据库设计者,如果你有命名的权限,我建议使用 tablename_id(cache_id) 并告诉程序员使用 ORM 配置来处理命名。这样,数据库开发人员和程序员都会感到高兴:
对于数据库开发人员和
程序员来说。
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
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:
and
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:
for the database developer and
for the programmer.