SQL Join Table Naming Convention

前端 未结 18 1751
面向向阳花
面向向阳花 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:24

    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
    
    0 讨论(0)
  • 2020-12-08 04:26

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

    User
    Role
    User2Role
    
    0 讨论(0)
  • 2020-12-08 04:29
    • 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
    0 讨论(0)
  • 2020-12-08 04:29

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

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

    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.

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

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

    user_role_join

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