SQL 连接表命名约定

发布于 2024-08-12 07:50:26 字数 363 浏览 5 评论 0原文

我有两个表:用户和角色,并且我有一个将这些表连接在一起的表。连接表中唯一的内容是链接两个表的 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 技术交流群。

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

发布评论

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

评论(18

悍妇囚夫 2024-08-19 07:50:26

映射表似乎存储了每个用户所属的所有角色。如果这是正确的,我将调用该表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.

情域 2024-08-19 07:50:26

我将用户表称为 User、角色表 Role 和联接表 UserRoles

顺便说一句,pk Id 在连接表中并不是真正必要的。最好将 UserIdRoleId 放在一起 pk 或只是 uk(唯一密钥),这样您就可以确保唯一的 User-Role< /代码> 关系。

I'd call the users table User, the roles table Role and the join table UserRoles.

By the way, the pk Id is not really necessary in a join table. Better make the UserId and RoleId together the pk or just uk (unique key) so that you can ensure unique User-Role relationships.

懵少女 2024-08-19 07:50:26

我建议简单地UsersRoles,因为这就是它所包含的内容。

I would suggest simply UsersRoles, as that is what it holds.

祁梦 2024-08-19 07:50:26
  • 表名应该始终是单数,这样以后您就不必像“是User还是Users?以S结尾的东西怎么样?等等” (如果您刚刚启动该项目,我现在会更改此设置)
  • 常见约定是:UserRole 和外部参照表:UserRole
  • 最重要的表或之前存在的表放在第一位。如果“角色”表在用户权限之外没有实际用途,则尤其如此。所以 UserRoleRoleUser 更有意义。
  • 如果您喜欢额外的冗长内容,我也见过类似 User_X_RoleUserXRole 的内容
  • Table names should always be singular, that way later you don't have to be like "is it User or Users? What about things that end in an S? etc" (I would change this now if you just started the project)
  • The common convention would be: User, Role, and the xref table: UserRole.
  • The most important table, or the one that existed before goes first. This is specially true if the 'role' table has no real use outside user permission stuff. so UserRole makes much more sense than RoleUser.
  • I've seen things like User_X_Role or UserXRole as well, if you like the extra verbosity
誰ツ都不明白 2024-08-19 07:50:26

数据库代表的是一个企业,对吧?那么那个企业的人怎么称呼这种关系呢?

以下是我所知道的一些内容:

  • 员工直线经理汇报 == 组织结构图

  • 学生参加课程 ==注册

  • 女人男人 结婚 == 婚姻

如有疑问,请咨询企业内的领域专家。

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 to line manager == org chart

  • student takes course == enrolment

  • woman marries man == marriages

When in doubt, ask a domain expert within the enterprise.

冷了相思 2024-08-19 07:50:26

我将链接表称为:

Remove_The_Surrogate_Primary_Key_From_The_Link_Table_Unless_You_Can_Prove_That_You_Really_Need_One

I'd call the link table this:

Remove_The_Surrogate_Primary_Key_From_The_Link_Table_Unless_You_Can_Prove_That_You_Really_Need_One
南汐寒笙箫 2024-08-19 07:50:26

我们有相同的结构并将链接表称为UserRoles。

We have the same structure and call the link table UserRoles.

心安伴我暖 2024-08-19 07:50:26

这是我工作场所的惯例:

UsersXRoles

This is the convention at my workplace:

UsersXRoles
伴我心暖 2024-08-19 07:50:26

我一直在仔细考虑这一点,我会将表 User 和表 Role 与表 UsersRoles 链接起来。我认为这很好,因为它表明多对多关系可以被视为将多个角色链接到一个用户,或者实际上将多个用户链接到一个角色(因此两者都是复数是有意义的)。它也可以理解为“用户的角色”,表明思考关系的正常方式是“用户具有的角色”。

I've been thinking carefully about this, and I would link the table User and the table Role with the table UsersRoles. 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.

很快妥协 2024-08-19 07:50:26

我总是选择类似的东西:rel_user_rolesrelUserRoles。哪个表先出现通常只取决于它在数据模型中的位置。

I've always gone with something like : rel_user_roles or relUserRoles. Which table goes first usually just depends on where it is in the data model.

贩梦商人 2024-08-19 07:50:26

2 种方法:

  1. 表之间只有一种关系:连接表可以是 RoleUser 或 Role_User。按照您的姓名顺序按字母顺序排列,角色第一,然后是用户,这样您就不必尝试记住!

  2. 表之间将有多个关系:relationshipname - 例如,您可能有用户的常规角色列表,并且可能有用户的潜在或过去角色列表。相同的两个表,但不同的关系。然后,您可能有 RoleUser_Current 和 RoleUser_Past。

2 approaches:

  1. 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!

  2. 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.

烟酒忠诚 2024-08-19 07:50:26

我有一个我发现很容易立即看到的约定:

User
Role
User2Role

I have a convention which I find easy to see right away:

User
Role
User2Role
把时间冻结 2024-08-19 07:50:26

RoleUser - 我使用字母顺序(即Role 位于User 之前)。这样,当您编写查询时,您就不必尝试记住用于命名连接表的顺序。

正如其他人提到的那样,我也使用单数 - 你不必尝试记住!对我有用。

RoleUser - I use alphabetic ordering (i.e. Role comes before User). 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.

(り薆情海 2024-08-19 07:50:26

我们总是使用两个表的名称,后跟“链接”一词。
因此,在您的示例中,我们的表名称将是“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'.

江挽川 2024-08-19 07:50:26

您可以从 Microsoft 窃取一个页面,并将其命名为“UsersInRoles”。

You could steal a page from Microsoft, and call it UsersInRoles.

葮薆情 2024-08-19 07:50:26

我尽量让事情变得简单,但也要具有描述性:

user_role_join

I try to keep things simple, but also be descriptive:

user_role_join

小伙你站住 2024-08-19 07:50:26

最好使用连接表的名称来命名连接表。
例如两个表“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.

不可一世的女人 2024-08-19 07:50:26

事实上,使用表别名并使用 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

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