Junction table

Junction table

In database management systems following the relational model, a junction table is a table that contains common fields from two or more tables. It is on the many side of a one-to-many relationship with each of the other tables. Junction tables are known under many names, among them cross-reference table, bridge table, join table, map table, intersection table, linking table, many-to-many resolver, link table, pairing table, pivot table, or association table.

Junction tables are employed when dealing with many-to-many relationships in a database. A practical use of a junction table would be to assign permissions to users. There can be multiple users, and each user can be assigned 0 or more permissions.

A visual depiction of the table schema described, with relationships indicated


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)
)

Using junction tables

A SELECT-statement on a junction table usually involves joining the main table with the junction table:

SELECT * FROM Users
JOIN UserPermissions USING (UserLogin);

This will return a list of all users and their permissions.

Inserting into a junction table involves two steps: first inserting into the main table (for example, a new User), then updating the junction table.

-- 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, PermissionsKey)
VALUES ('SomeUser', 'TheKey');

Using foreign keys, the database will automatically dereference the values of the UserPermissions table to their own table.

See also



Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Table of United States Metropolitan Statistical Areas — An enlargeable map of the 942 Core Based Statistical Areas (CBSAs) of the United States. The 367 Metropolitan Statistical Areas (MSAs) are shown in red. Main articles: Metropolitan Statistical Area and Core Based Statistical Area …   Wikipedia

  • Summary of Road Traffic Control Devices, Table — ▪ Table Signs Guide Regulatory Numbered highway route marker Stop Junction marker Yield Bypass/alternate/business/truck route marker Speed limit Detour Minimum speed Trail blazer Turn prohibition Destination Lane use control Street name Passing… …   Universalium

  • Bahnstrecke White River Junction–Lennoxville — White River Junction VT– Lennoxville QC[1][2][3][4] Streckenlänge: 227 km Spurweite: 1435 mm (Normalspur) Legende …   Deutsch Wikipedia

  • Grand Junction Metropolitan Statistical Area — The Grand Junction Metropolitan Statistical Area is a United States Census Bureau defined Metropolitan Statistical Area (MSA) located in the Grand Junction region of the State of Colorado. The Grand Junction Metropolitan Statistical Area is… …   Wikipedia

  • Bahnstrecke Montpelier Junction–Williamstown — Montpelier Junction VT– Williamstown VT[1][2] Gesellschaft: WACR Streckenlänge: 24 km Spurweite: 1435 mm (Normalspur) Legende …   Deutsch Wikipedia

  • Otis Junction, Oregon — Otis Junction is an unincorporated locale in Lincoln County, Oregon, United States, at the junction of the former route of U.S. Route 101 and Oregon Route 18, a half mile south of Otis.[1][2][3] It is a mile and a half east of where OR 18… …   Wikipedia

  • Cave Junction, Oregon — Infobox Settlement official name = Cave Junction, Oregon settlement type = City nickname = motto = Gateway to the Oregon Caves imagesize = image caption = image imagesize = image caption = image mapsize = 250x200px map caption = Location in… …   Wikipedia

  • Cave Junction — 42° 10′ 00″ N 123° 38′ 49″ W / 42.16666667, 123.64694444 …   Wikipédia en Français

  • Coombe Junction Halt railway station — This article is about the station on the Looe Valley Line in Cornwall. For that in Oxfordshire see Combe railway station. Coombe Junction Halt …   Wikipedia

  • Ambergate, Nottingham, Boston and Eastern Junction Railway — MetaSidebar|30%|#eeffff|right|Stations and Landmarks Nottingham London Road Colwick Junction Netherfield Radcliffe on Trent Bingham Aslockton Elton and Orston Bottesford East Sedgebrook Grantham Ambergate Yard Grantham The Ambergate, Nottingham,… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”