on
[Sequelize - 개념 공부] Junction table
[Sequelize - 개념 공부] Junction table
Junction table ?
: A와 B의 FK가 있어 데이터 데이블의 기본기를 참조해 둘 이상의 테이블을 함께 매핑하는 것.
associative entity에 나오는 용어로 DB 테이블 Association에서 BelongsToMany association 공부를 할 때 나온 개념이다.
Using associative tables
CREATE TABLE Users ( UserLogin varchar(50) PRIMARY KEY, UserPassword varchar(50) NOT NULL, UserName varchar(50) NOT NULL ); CREATE TABLE Permissions ( PermissionKey varchar(50) PRIMARY KEY, PermissionDescription varchar(500) NOT NULL ); -- This is the junction table. CREATE TABLE UserPermissions ( UserLogin varchar(50) REFERENCES Users (UserLogin), PermissionKey varchar(50) REFERENCES Permissions (PermissionKey), PRIMARY KEY (UserLogin, PermissionKey) );
SELECT문 : 일반적으로 메인 테이블을 junction 테이블과 조인하는 것을 포함한다.-> 모든 사용자 및 해당 권한의 목록 반환
SELECT * FROM 사용자 JOIN UserPermissions USING ( UserLogin );
INSERT문 : juncton 테이블에 정보 추가 (1. User 테이블 생성, Permission테이블 생성, junction 테이블 업데이트
-- Creating a new User INSERT INTO Users (UserLogin, UserPassword, UserName) VALUES ('SomeUser', 'SecretPassword', 'UserName'); -- Creating a new Permission INSERT INTO Permissions (PermissionKey, PermissionDescription) VALUES ('TheKey', 'A key used for several permissions'); -- Finally, updating the junction INSERT INTO UserPermissions (UserLogin, PermissionKey) VALUES ('SomeUser', 'TheKey');
DB는 FK를 사용해 자동으로 UserPermission table의 값을 자기 테이블로 dereference (역참조)한다.
참고 : junction table의 다른 이름
association table, bridge table, cross-reference table, crosswalk, intermediary table, intersection table, join table, junction table, link table, linking table, many-to-many resolver, map table, mapping table, pairing table, pivot table (as used incorrectly in Laravel - not to be confused with the correct use of pivot table in spreadsheets), or transition table.
참고 출처 : https://en.wikipedia.org/wiki/Associative_entity
from http://doplinblue.tistory.com/15 by ccl(A) rewrite - 2021-07-15 15:26:55