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
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.
I would suggest simply UsersRoles, as that is what it holds.
Indeed, use table aliases and select the columns with same names apart with the AS selector.
e.g. SELECT user.id AS user_id
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.
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.
You could steal a page from Microsoft, and call it UsersInRoles.