SQL 连接表命名约定
我有两个表:用户和角色,并且我有一个将这些表连接在一起的表。连接表中唯一的内容是链接两个表的 ID。
我应该给这张桌子起什么名字?我从来没有真正见过一个好的命名约定。
我以前见过的约定:
- UsersToRolesJoin
- UsersToRolesLink
- UsersToRolesMembership
- UsersRoles
例如:
Users:
Id
Name
Roles:
Id
Name
TableThatJoinsTheTwo:
Id
UserId
RoleId
I have 2 tables: Users and Roles, and I have a table that joins these together. The only thing in the join table is Ids that link the 2 tables.
What should I call this table? I've never really seen a good naming convention for this.
Conventions I've seen before:
- UsersToRolesJoin
- UsersToRolesLink
- UsersToRolesMembership
- UsersRoles
Ex:
Users:
Id
Name
Roles:
Id
Name
TableThatJoinsTheTwo:
Id
UserId
RoleId
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
映射表似乎存储了每个用户所属的所有角色。如果这是正确的,我将调用该表
UserRoles
。这正确地(IMO)复数了表的意图,而不是听起来很奇怪的
UsersRoles
。It seems like the mapping table is storing all the roles that each user is a member of. If this is correct I would call the table
UserRoles
.This correctly (IMO) pluralizes the intent of the table rather than
UsersRoles
which just sounds odd.我将用户表称为
User
、角色表Role
和联接表UserRoles
。顺便说一句,pk
Id
在连接表中并不是真正必要的。最好将UserId
和RoleId
放在一起 pk 或只是 uk(唯一密钥),这样您就可以确保唯一的User
-Role< /代码> 关系。
I'd call the users table
User
, the roles tableRole
and the join tableUserRoles
.By the way, the pk
Id
is not really necessary in a join table. Better make theUserId
andRoleId
together the pk or just uk (unique key) so that you can ensure uniqueUser
-Role
relationships.我建议简单地UsersRoles,因为这就是它所包含的内容。
I would suggest simply UsersRoles, as that is what it holds.
User
还是Users
?以S结尾的东西怎么样?等等” (如果您刚刚启动该项目,我现在会更改此设置)User
、Role
和外部参照表:UserRole
。UserRole
比RoleUser
更有意义。User_X_Role
或UserXRole
的内容User
orUsers
? What about things that end in an S? etc" (I would change this now if you just started the project)User
,Role
, and the xref table:UserRole
.UserRole
makes much more sense thanRoleUser
.User_X_Role
orUserXRole
as well, if you like the extra verbosity数据库代表的是一个企业,对吧?那么那个企业的人怎么称呼这种关系呢?
以下是我所知道的一些内容:
员工
向直线经理
汇报 == 组织结构图学生
参加课程
==注册女人
与男人
结婚 == 婚姻如有疑问,请咨询企业内的领域专家。
The database represents am enterprise, right? So what do the people in that enterprise call the relationship?
Here are some I do know:
employee
reports toline manager
== org chartstudent
takescourse
== enrolmentwoman
marriesman
== marriagesWhen in doubt, ask a domain expert within the enterprise.
我将链接表称为:
I'd call the link table this:
我们有相同的结构并将链接表称为UserRoles。
We have the same structure and call the link table UserRoles.
这是我工作场所的惯例:
This is the convention at my workplace:
我一直在仔细考虑这一点,我会将表
User
和表Role
与表UsersRoles
链接起来。我认为这很好,因为它表明多对多关系可以被视为将多个角色链接到一个用户,或者实际上将多个用户链接到一个角色(因此两者都是复数是有意义的)。它也可以理解为“用户的角色”,表明思考关系的正常方式是“用户具有的角色”。I've been thinking carefully about this, and I would link the table
User
and the tableRole
with the tableUsersRoles
. I think its nice, because it indicates that the many-to-many relationship could be considered as linking many roles to one user, or indeed many users to one role (so both being plural makes sense). It can also be read as "Users' roles", indicating that the normal way of thinking about the relationship is the "roles that a user has" way round.我总是选择类似的东西:
rel_user_roles
或relUserRoles
。哪个表先出现通常只取决于它在数据模型中的位置。I've always gone with something like :
rel_user_roles
orrelUserRoles
. Which table goes first usually just depends on where it is in the data model.2 种方法:
表之间只有一种关系:连接表可以是 RoleUser 或 Role_User。按照您的姓名顺序按字母顺序排列,角色第一,然后是用户,这样您就不必尝试记住!
表之间将有多个关系:relationshipname - 例如,您可能有用户的常规角色列表,并且可能有用户的潜在或过去角色列表。相同的两个表,但不同的关系。然后,您可能有 RoleUser_Current 和 RoleUser_Past。
2 approaches:
where you will only ever have one relationship between the tables: join table could be RoleUser or Role_User. Go alphabetic with your name order, Role 1st, then User, then you don't have to try to remember!
where you will have multiple relationships between the tables: relationshipname - e.g. you might have a list of regular roles for users, and you might have a list of potential, or past roles for users. Same 2 tables, but different relationships. Then you might have RoleUser_Current and RoleUser_Past.
我有一个我发现很容易立即看到的约定:
I have a convention which I find easy to see right away:
RoleUser
- 我使用字母顺序(即Role
位于User
之前)。这样,当您编写查询时,您就不必尝试记住用于命名连接表的顺序。正如其他人提到的那样,我也使用单数 - 你不必尝试记住!对我有用。
RoleUser
- I use alphabetic ordering (i.e.Role
comes beforeUser
). That way when you're writing a query you won't have to try and remember which order you used to name the join table.I also use singular as someone else mentioned - you don't have to try to remember! Works for me.
我们总是使用两个表的名称,后跟“链接”一词。
因此,在您的示例中,我们的表名称将是“UsersRolesLinks”。
We've always used the names of the two tables followed by the word, 'Links'.
So in your example our table name would be 'UsersRolesLinks'.
您可以从 Microsoft 窃取一个页面,并将其命名为“UsersInRoles”。
You could steal a page from Microsoft, and call it UsersInRoles.
我尽量让事情变得简单,但也要具有描述性:
user_role_join
I try to keep things simple, but also be descriptive:
user_role_join
最好使用连接表的名称来命名连接表。
例如两个表“products”和“product_tags”,连接表称为“product_product_tags”。
最大的优点是,从这个名称中您可以立即说出哪些表连接在一起。当您的数据库中有 50 个或更多表时,这样就好了,您不再需要考虑连接表的目的。
Its good to name join table by using names of tables which it connects.
For example two tables "products" and "product_tags", and the joining table is called "product_product_tags".
The big advantage is that from this name you can immediatelly say which tables its joining together. When you have 50 and more tables in your DB its good to have it like this and you no longer need to think about joining tables purposes.
事实上,使用表别名并使用 AS 选择器选择具有相同名称的列。
例如
SELECT user.id AS user_id
Indeed, use table aliases and select the columns with same names apart with the AS selector.
e.g.
SELECT user.id AS user_id