Lably
Skip to content

Foreign key

Concept in database systems

  • 12 min read
  • 3 chapters
  • 1 lab on Lably

Learn it on Lably

In other words, a foreign key is a set of attributes that references 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.

Summary

The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table. In database relational modeling and implementation, a candidate key is a set of zero or more attributes, the values of which are guaranteed to be unique for each tuple (row) in a relation. The value or combination of values of candidate key attributes for any tuple cannot be duplicated for any other tuple in that relation.

Since the purpose of the foreign key is to identify a particular row of referenced table, it is generally required that the foreign key is equal to the candidate key in some row of the primary table, or else have no value (the NULL value.). This rule is called a referential integrity constraint between the two tables. Because violations of these constraints can be the source of many database problems, most database management systems provide mechanisms to ensure that every non-null foreign key corresponds to a row of the referenced table.

For example, consider a database with two tables: a CUSTOMER table that includes all customer data and an ORDER table that includes all customer orders. Suppose the business requires that each order must refer to a single customer. To reflect this in the database, a foreign key column is added to the ORDER table (e.g., CUSTOMERID), which references the primary key of CUSTOMER (e.g. ID). Because the primary key of a table must be unique, and because CUSTOMERID only contains values from that primary key field, we may assume that, when it has a value, CUSTOMERID will identify the particular customer which placed the order. However, this can no longer be assumed if the ORDER table is not kept up to date when rows of the CUSTOMER table are deleted or the ID column altered, and working with these tables may become more difficult. Many real world databases work around this problem by 'inactivating' rather than physically deleting master table foreign keys, or by complex update programs that modify all references to a foreign key when a change is needed.

Foreign keys play an essential role in database design. One important part of database design is making sure that relationships between real-world entities are reflected in the database by references, using foreign keys to refer from one table to another. Another important part of database design is database normalization, in which tables are broken apart and foreign keys make it possible for them to be reconstructed.

Multiple rows in the referencing (or child) table may refer to the same row in the referenced (or parent) table. In this case, the relationship between the two tables is called a one to many relationship between the referencing table and the referenced table.

In addition, the child and parent table may, in fact, be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known in SQL:2003 as a self-referencing or recursive foreign key. In database management systems, this is often accomplished by linking a first and second reference to the same table.

A table may have multiple foreign keys, and each foreign key can have a different parent table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.

A foreign key is defined as an attribute or set of attributes in a relation whose values match a primary key in another relation. The syntax to add such a constraint to an existing table is defined in SQL:2003 as shown below. Omitting the column list in the REFERENCES clause implies that the foreign key shall reference the primary key of the referenced table. Likewise, foreign keys can be defined as part of the CREATE TABLE SQL statement.

<span id="mwUg"></span><span class="k" id="mwUw">CREATE</span><span class="w" id="mwVA"> </span><span class="k" id="mwVQ">TABLE</span><span class="w" id="mwVg"> </span><span class="n" id="mwVw">child_table</span><span class="w" id="mwWA"> </span><span class="p" id="mwWQ">(</span> <span class="w" id="mwWg"> </span><span class="n" id="mwWw">col1</span><span class="w" id="mwXA"> </span><span class="nb" id="mwXQ">INTEGER</span><span class="w" id="mwXg"> </span><span class="k" id="mwXw">PRIMARY</span><span class="w" id="mwYA"> </span><span class="k" id="mwYQ">KEY</span><span class="p" id="mwYg">,</span> <span class="w" id="mwYw"> </span><span class="n" id="mwZA">col2</span><span class="w" id="mwZQ"> </span><span class="nb" id="mwZg">CHARACTER</span><span class="w" id="mwZw"> </span><span class="nb" id="mwaA">VARYING</span><span class="p" id="mwaQ">(</span><span class="mi" id="mwag">20</span><span class="p" id="mwaw">),</span> <span class="w" id="mwbA"> </span><span class="n" id="mwbQ">col3</span><span class="w" id="mwbg"> </span><span class="nb" id="mwbw">INTEGER</span><span class="p" id="mwcA">,</span> <span class="w" id="mwcQ"> </span><span class="n" id="mwcg">col4</span><span class="w" id="mwcw"> </span><span class="nb" id="mwdA">INTEGER</span><span class="p" id="mwdQ">,</span> <span class="w" id="mwdg"> </span><span class="k" id="mwdw">FOREIGN</span><span class="w" id="mweA"> </span><span class="k" id="mweQ">KEY</span><span class="p" id="mweg">(</span><span class="n" id="mwew">col3</span><span class="p" id="mwfA">,</span><span class="w" id="mwfQ"> </span><span class="n" id="mwfg">col4</span><span class="p" id="mwfw">)</span><span class="w" id="mwgA"> </span><span class="k" id="mwgQ">REFERENCES</span><span class="w" id="mwgg"> </span><span class="n" id="mwgw">parent_table</span><span class="p" id="mwhA">(</span><span class="n" id="mwhQ">col1</span><span class="p" id="mwhg">,</span><span class="w" id="mwhw"> </span><span class="n" id="mwiA">col2</span><span class="p" id="mwiQ">)</span><span class="w" id="mwig"> </span><span class="k" id="mwiw">ON</span><span class="w" id="mwjA"> </span><span class="k" id="mwjQ">DELETE</span><span class="w" id="mwjg"> </span><span class="k" id="mwjw">CASCADE</span> <span class="p" id="mwkA">)</span>

If the foreign key is a single column only, the column can be marked as such using the following syntax:

<span id="mwlA"></span><span class="k" id="mwlQ">CREATE</span><span class="w" id="mwlg"> </span><span class="k" id="mwlw">TABLE</span><span class="w" id="mwmA"> </span><span class="n" id="mwmQ">child_table</span><span class="w" id="mwmg"> </span><span class="p" id="mwmw">(</span> <span class="w" id="mwnA"> </span><span class="n" id="mwnQ">col1</span><span class="w" id="mwng"> </span><span class="nb" id="mwnw">INTEGER</span><span class="w" id="mwoA"> </span><span class="k" id="mwoQ">PRIMARY</span><span class="w" id="mwog"> </span><span class="k" id="mwow">KEY</span><span class="p" id="mwpA">,</span> <span class="w" id="mwpQ"> </span><span class="n" id="mwpg">col2</span><span class="w" id="mwpw"> </span><span class="nb" id="mwqA">CHARACTER</span><span class="w" id="mwqQ"> </span><span class="nb" id="mwqg">VARYING</span><span class="p" id="mwqw">(</span><span class="mi" id="mwrA">20</span><span class="p" id="mwrQ">),</span> <span class="w" id="mwrg"> </span><span class="n" id="mwrw">col3</span><span class="w" id="mwsA"> </span><span class="nb" id="mwsQ">INTEGER</span><span class="p" id="mwsg">,</span> <span class="w" id="mwsw"> </span><span class="n" id="mwtA">col4</span><span class="w" id="mwtQ"> </span><span class="nb" id="mwtg">INTEGER</span><span class="w" id="mwtw"> </span><span class="k" id="mwuA">REFERENCES</span><span class="w" id="mwuQ"> </span><span class="n" id="mwug">parent_table</span><span class="p" id="mwuw">(</span><span class="n" id="mwvA">col1</span><span class="p" id="mwvQ">)</span><span class="w" id="mwvg"> </span><span class="k" id="mwvw">ON</span><span class="w" id="mwwA"> </span><span class="k" id="mwwQ">DELETE</span><span class="w" id="mwwg"> </span><span class="k" id="mwww">CASCADE</span> <span class="p" id="mwxA">)</span>

Foreign keys can be defined with a stored procedure statement.

<span id="mwyQ"></span><span class="n" id="mwyg">sp_foreignkey</span><span class="w" id="mwyw"> </span><span class="n" id="mwzA">child_table</span><span class="p" id="mwzQ">,</span><span class="w" id="mwzg"> </span><span class="n" id="mwzw">parent_table</span><span class="p" id="mw0A">,</span><span class="w" id="mw0Q"> </span><span class="n" id="mw0g">col3</span><span class="p" id="mw0w">,</span><span class="w" id="mw1A"> </span><span class="n" id="mw1Q">col4</span>
  • 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.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication