An associative entity is a term used in relational and entity–relationship theory. A relational database requires the implementation of a base relation to resolve many-to-many relationships. A base relation representing this kind of entity is called, informally, an associative table. As mentioned above, associative entities are implemented in a database structure using associative tables, which are tables that can contain references to columns from the same or different database tables within the same database. An associative table maps two or more tables together by referencing the primary keys of each data table. In effect, it contains a number of foreign keys, each in a many-to-one relationship from the junction table to the individual data tables. The PK of the associative table is typically composed of the FK columns themselves. Associative tables are colloquially known under many names, including 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, or transition table.
Using associative tables
An example of the practical use of an associative table would be to assign permissions to users. There can be multiple users, and each user can be assigned zero or more permissions. Individual permissions may be granted to one or more users. CREATE TABLE Users PRIMARY KEY, UserPassword varchar NOT NULL, UserName varchar NOT NULL ); CREATE TABLE Permissions PRIMARY KEY, PermissionDescription varchar NOT NULL ); -- This is the junction table. CREATE TABLE UserPermissions REFERENCES Users, PermissionKey varchar REFERENCES Permissions, PRIMARY KEY );
A SELECT-statement on a junction table usually involves joining the main table with the junction table: SELECT * FROM Users JOIN UserPermissions USING ;
This will return a list of all users and their permissions. Inserting into a junction table involves multiple steps: first inserting into the main table, then updating the junction table. -- Creating a new User INSERT INTO Users VALUES ; -- Creating a new Permission INSERT INTO Permissions VALUES ; -- Finally, updating the junction INSERT INTO UserPermissions VALUES ;
Using foreign keys, the database will automatically dereference the values of the UserPermissions table to their own tables.