SQL Join Table Naming Convention

前端 未结 18 1750
面向向阳花
面向向阳花 2020-12-08 04:01

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 th

相关标签:
18条回答
  • 2020-12-08 04:09

    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.

    0 讨论(0)
  • 2020-12-08 04:09

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

    0 讨论(0)
  • 2020-12-08 04:11

    Indeed, use table aliases and select the columns with same names apart with the AS selector.

    e.g. SELECT user.id AS user_id

    0 讨论(0)
  • 2020-12-08 04:12

    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.

    0 讨论(0)
  • 2020-12-08 04:13

    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.

    0 讨论(0)
  • 2020-12-08 04:14

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

    0 讨论(0)
提交回复
热议问题