Programing

SQL 조인 테이블 명명 규칙

lottogame 2020. 12. 1. 07:33
반응형

SQL 조인 테이블 명명 규칙


사용자와 역할이라는 두 개의 테이블이 있고 이들을 결합하는 테이블이 있습니다. 조인 테이블의 유일한 것은 두 테이블을 연결하는 ID입니다.

이 테이블을 무엇이라고 부를까요? 나는 이것에 대한 좋은 명명 규칙을 본 적이 없습니다.

이전에 본 규칙 :

  • UsersToRolesJoin
  • UsersToRolesLink
  • UsersToRolesMembership
  • UsersRoles

전의:

Users:  
Id  
Name

Roles:  
Id  
Name  

TableThatJoinsTheTwo:  
Id  
UserId  
RoleId  

매핑 테이블은 각 사용자가 구성원 인 모든 역할을 저장하는 것 같습니다. 이것이 맞다면 나는 테이블을 부를 것이다 UserRoles.

이것은 정확하게 (IMO) UsersRoles이상하게 들리는 것보다 테이블의 의도를 복수화합니다 .


사용자 테이블 User, 역할 테이블 Role및 조인 테이블을 호출 합니다 UserRoles.

그건 그렇고, pk Id는 조인 테이블에서 실제로 필요하지 않습니다. 더 나은은을 UserIdRoleId함께 PK하거나 영국 (고유 키)는 그래서 당신은 고유 보장 할 수 있음 User- Role관계를.


나는 단순히 UsersRoles를 제안 할 것 입니다.


링크 테이블을 다음과 같이 호출합니다.

Remove_The_Surrogate_Primary_Key_From_The_Link_Table_Unless_You_Can_Prove_That_You_Really_Need_One

  • 테이블 이름은 항상 단수 여야합니다. 이렇게하면 나중에 "그렇 User거나 Users? S로 끝나는 것은 어떻습니까? 등" 처럼 될 필요가 없습니다 (프로젝트를 막 시작한 경우 지금 변경하겠습니다).
  • 일반적인 규칙은 다음과 같습니다 User, Role그리고 외부 참조 테이블 : UserRole.
  • 가장 중요한 테이블 또는 이전에 존재했던 테이블이 먼저 표시됩니다. '역할'테이블이 사용자 권한 외부에서 실제로 사용되지 않는 경우 특히 그렇습니다. 그래서 UserRole보다 훨씬 더 많은의 의미가 있습니다 RoleUser.
  • 내가 좋아하는 것을 본 적이 User_X_Role또는 UserXRole당신이 경우 추가 상세처럼뿐만 아니라,

우리는 동일한 구조를 가지고 있으며 링크 테이블을 UserRoles라고 부릅니다.


데이터베이스는 엔터프라이즈를 나타냅니다. 맞죠? 그렇다면 그 기업의 사람들은 관계를 무엇이라고 부릅니까?

내가 아는 몇 가지는 다음과 같습니다.

  • employeeline manager== 조직도에 보고

  • studentcourse== 등록 소요

  • woman결혼 man== 결혼

확실하지 않은 경우 기업 내 도메인 전문가에게 문의하십시오.


이것은 제 직장에서의 컨벤션입니다.

UsersXRoles

나는 항상 : rel_user_roles또는 relUserRoles. 어떤 테이블이 먼저 들어가는지는 일반적으로 데이터 모델의 위치에 따라 다릅니다.


나는 이것에 대해 신중하게 생각 User하고 있으며 테이블 Role테이블 테이블 연결 합니다 UsersRoles. 다 대다 관계가 많은 역할을 한 사용자에 연결하거나 실제로 많은 사용자를 하나의 역할에 연결하는 것으로 간주 될 수 있음을 나타 내기 때문에 좋은 것 같습니다 (둘 다 복수 인 것이 의미가 있음). "사용자의 역할"로 읽을 수도 있습니다. 이는 관계에 대한 일반적인 사고 방식이 "사용자가 갖는 역할"이라는 것을 나타냅니다.


즉시 쉽게 볼 수있는 컨벤션이 있습니다.

User
Role
User2Role

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.


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.


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


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


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

user_role_join


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.


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

e.g. SELECT user.id AS user_id

참고URL : https://stackoverflow.com/questions/1764483/sql-join-table-naming-convention

반응형