encyclopegia.org

Foreign key

A foreign key is a set of attributes in a table that refers to the primary key of another table, linking these two tables. In the context of relational databases, a foreign key is subject to an inclusion dependency constraint that the tuples consisting of the foreign key attributes in one relation, R, must also exist in some other (not necessarily distinct) relation, S; furthermore that those attributes must also be a candidate key in S. In other words, a foreign key is a set of attributes that a candidate key. For example, a table called TEAM may have an attribute, MEMBER_NAME, which is a foreign key referencing a candidate key, PERSON_NAME, in the PERSON table. Since MEMBER_NAME is a foreign key, any value existing as the name of a member in TEAM must also exist as a person's name in the PERSON table; in other words, every member of a TEAM is also a PERSON.

A foreign key is a set of attributes in a table that refers to the primary key of another table, linking these two tables. In the context of relational databases, a foreign key is subject to an inclusion dependency constraint that the tuples consisting of the foreign key attributes in one relation, R, must also exist in some other (not necessarily distinct) relation, S; furthermore that those attributes must also be a candidate key in S. In other words, a foreign key is a set of attributes that a candidate key. For example, a table called TEAM may have an attribute, MEMBER_NAME, which is a foreign key referencing a candidate key, PERSON_NAME, in the PERSON table. Since MEMBER_NAME is a foreign key, any value existing as the name of a member in TEAM must also exist as a person's name in the PERSON table; in other words, every member of a TEAM is also a PERSON. CREATE TABLE child_table ( col1 INTEGER PRIMARY KEY, col2 CHARACTER VARYING(20), col3 INTEGER, col4 INTEGER, FOREIGN KEY(col3, col4) REFERENCES parent_table(col1, col2) ON DELETE CASCADE ) If the foreign key is a single column only, the column can be marked as such using the following syntax: CREATE TABLE child_table ( col1 INTEGER PRIMARY KEY, col2 CHARACTER VARYING(20), col3 INTEGER, col4 INTEGER REFERENCES parent_table(col1) ON DELETE CASCADE ) Foreign keys can be defined with a stored procedure statement. sp_foreignkey child_table, parent_table, col3, col4 child_table: the name of the table or view that contains the foreign key to be defined. parent_table: the name of the table or view that has the primary key to which the foreign key applies. The primary key must already be defined. col3 and col4: the name of the columns that make up the foreign key. The foreign key must have at least one column and at most eight columns. CREATE TABLE account (acct_num INT, amount DECIMAL(10,2)); CREATE TRIGGER ins_sum BEFORE INSERT ON account FOR EACH ROW SET @sum = @sum + NEW.amount; CREATE TABLE Supplier ( SupplierNumber INTEGER NOT NULL, Name VARCHAR(20) NOT NULL, Address VARCHAR(50) NOT NULL, CONSTRAINT supplier_pk PRIMARY KEY(SupplierNumber), CONSTRAINT number_value CHECK(SupplierNumber > 0) ) CREATE TABLE Invoice ( InvoiceNumber INTEGER NOT NULL, Text VARCHAR(4096), SupplierNumber INTEGER NOT NULL, CONSTRAINT invoice_pk PRIMARY KEY(InvoiceNumber), CONSTRAINT inumber_value CHECK (InvoiceNumber > 0), CONSTRAINT supplier_fk FOREIGN KEY(SupplierNumber) REFERENCES Supplier(SupplierNumber) ON UPDATE CASCADE ON DELETE RESTRICT )

References

No reference rows published for this article.

Cite this article

MLA

"Foreign key." encyclopegia.org, 2025, https://encyclopegia.org/wiki/foreign-key. Accessed 24 November 2026.

Chicago

"Foreign key." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/foreign-key.

APA

encyclopegia.org. (2025). Foreign key. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/foreign-key

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Glass Key award

stub · references available

Key Falls

stub · references available

foreign exchange market

stub · references available

Foreign Intelligence Service

stub · references available

Federal Public Service Foreign Affairs

stub · references available

foreign government advisor in Meiji Japan

stub · references available

Foreign Legion

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1270418735 · 0.5 · snapshot enwiki-001270418735

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.