Mysql 数据库设计-主键命名约定

Mysql 数据库设计-主键命名约定,mysql,database,Mysql,Database,我很想知道人们对MySQL中命名数据库表主键的以下3种不同约定的看法(以及原因) -例1- 表名:用户, 主键列名:用户\u id -例2- 表名:用户, 主键列名:id -例3- 表名:用户, 主键列名:主键用户id 只是想听一些想法,或许在这个过程中可以学到一些东西:) 谢谢。我一直很欣赏Justinsomnia对数据库命名约定的理解。请阅读:我建议使用示例2。这样,外键和主键之间就不会有歧义,如示例1所示。例如,你可以这样做 SELECT * FROM user, comment WHER

我很想知道人们对MySQL中命名数据库表主键的以下3种不同约定的看法(以及原因)

-例1-

表名:用户,
主键列名:用户\u id

-例2-

表名:用户,
主键列名:id

-例3-

表名:用户,
主键列名:主键用户id

只是想听一些想法,或许在这个过程中可以学到一些东西:)


谢谢。

我一直很欣赏Justinsomnia对数据库命名约定的理解。请阅读:

我建议使用示例2。这样,外键和主键之间就不会有歧义,如示例1所示。例如,你可以这样做

SELECT * FROM user, comment WHERE user.id = comment.user_id
简洁明了


第三个例子在所有id都用作主键的设计中是多余的。

我选择选项2。对我来说,“id”本身就足够了。 由于该表是用户,因此“用户”中的“id”列表示它是用户的标识标准

然而,我必须补充一点,命名约定都是关于一致性的。
只要有一个一致的模式并在整个应用程序中应用,通常就没有对错之分。这可能是命名约定的有效性以及它们在使应用程序更易于理解和维护方面的作用的更重要的因素。

我总是喜欢示例1中的选项,其中表名(冗余)用于列名。这是因为我更喜欢在连接中查看user.id=history.user\u id上的
,而不是user.id=history.user\u id

然而,在Stackoverflow上,在这个问题上的观点权重似乎与我的观点相反,大多数人更喜欢示例2


顺便说一句,作为列命名约定,我更喜欢UserID而不是user\u id。我不喜欢键入下划线,使用下划线作为常见的SQL单字符匹配字符有时会有点混乱。

我倾向于使用第一个选项,
user\u id

如果使用
id
,您通常需要在查询中过度使用别名。 如果你选择了更复杂的id,那么你要么必须缩写,要么就没有空间了,你会厌倦输入这么长的列名


2美分。

我同意@indane and like
Id
。原因如下:

如果您有一个名为User的表,以及一个处理用户名的列,您是将其称为UserName还是仅称为name?“用户”似乎是多余的。如果您有一个名为Customer的表和一个名为Address的列,您会调用CustomerAddress列吗


虽然我也看到了在哪里使用
UserId
,如果您有一个带有外键的表给User,那么该列也将是UserId。这允许在命名上保持一致性,但在我看来,这并不能给你带来太多的好处。

好吧,那么忘记例子3吧——这很愚蠢,所以它介于1和2之间

PK学派的id(2)

PK/FK名称思想流派的tablename\u id前缀(1)

(请随意使用表名的缩写形式,即cust_id而不是customer_id)

所以你可以看到tablename_uu前缀或缩写tablename_u前缀方法是ofc最常用的
始终如一且容易成为最佳惯例

ID是我认为最糟糕的PK名称。TablenameID在报告方面工作得更好,因此在执行复杂的报告查询时,您不必为一组名称相同的列添加别名


我个人认为,只有当列的意思相同时,才应该将它们命名为相同的东西。客户ID与orderid的含义不同,因此它们在概念上应该有不同的名称。当您有许多连接和复杂的数据结构时,如果pk和fk具有相同的名称,那么维护起来也会更容易。当您有ID列时,很难在联接中发现错误。例如,假设您加入了四个表,所有这些表都有一个ID列。在上一次联接中,您意外地将别名用于第一个表,而不是第三个表。如果使用OrderID、CustomerID等代替ID,则会出现语法错误,因为第一个表不包含该列。如果您使用ID,它将很高兴错误地加入

对于Tomas的回答,假设注释表的主键也被命名为id,则仍然存在歧义

在回答这个问题时,示例1得到了我的投票[table name]\u id实际上可以消除歧义

而不是

选择u.id作为用户id,选择c.id作为注释id,从用户u加入注释c ON u.id=c.user\u id

我可以简单地写

选择user\u id,comment\u id FROM user u JOIN comment c ON u.user\u id=c.user\u id


WHEREON中使用相同的ID名称没有任何歧义。这实际上增加了对IMHO的了解

嘿,对不起,我只喜欢tablename\u id:p这里有点混乱。从c.id=o.cid的客户c内部联接订单o中选择c.id、o.id;两个列都具有名称id,因此一个是id,另一个是id1-请参阅我的postImplicit联接是SQL反模式。你不应该使用这种模式超过20年,这种方法是不一致的。从c.id=o.cid上的客户c内部联接订单o中选择c.id、o.id。此查询输出的两个字段的名称是什么?因此,您必须使用别名,因此别名命名可能不一致conventions@f00-当我提到一致性时,我指的是能够依赖这样一个事实,即数据库中任何表中的“id”列在表中代表相同的内容。关于你关于别名的观点,通常会有一个标准
drop table if exists customer;
create table customer
(
id int unsigned not null auto_increment primary key, -- my names are id, cid, cusid, custid ????
name varchar(255) not null
)engine=innodb;

insert into customer (name) values ('cust1'),('cust2');

drop table if exists orders;

create table orders
(
id int unsigned not null auto_increment primary key, -- my names are id, oid, ordid
cid int unsigned not null -- hmmm what shall i call this ?
)engine=innodb;

insert into orders (cid) values (1),(2),(1),(1),(2);

-- so if i do a simple give me all of the customer orders query we get the following output

select
 c.id,
 o.id
from
 customer c
inner join orders o on c.id = o.cid;

id  id1 -- big fan of column names like id1, id2, id3 : they are sooo descriptive
==  ===
1     1
2     2
1     3
1     4
2     5

-- so now i have to alias my columns like so:

select
 c.id as cid, -- shall i call it cid or custid, customer_id whatever ??
 o.id as oid
from
 customer c
inner join orders o on c.id = o.cid; -- cid here but id in customer - where is my consistency ?

cid oid 
==  ===
1     1
2     2
1     3
1     4
2     5
drop table if exists customer;
create table customer
(
cust_id int unsigned not null auto_increment primary key, -- pk
name varchar(255) not null
)engine=innodb;

insert into customer (name) values ('cust1'),('cust2');

drop table if exists orders;
create table orders
(
order_id int unsigned not null auto_increment primary key,
cust_id int unsigned not null 
)engine=innodb;

insert into orders (cust_id) values (1),(2),(1),(1),(2);

select
 c.cust_id,
 o.order_id
from
 customer c
inner join orders o on c.cust_id = o.cust_id; -- ahhhh, cust_id is cust_id is cust_id :)

cust_id order_id
======= ========
1           1
2           2
1           3
1           4
2           5