Example (1)
A customer table contains a row for each customer. Each customer row has a foreign key called ADDRESS_ID that points to the customer's address.Example (2)
A star table contains a row for each known star. Each star row has a foreign key called GALAXY_ID that points to the star's Galaxy.Example (3)
An actor table contains a row for each actor in a movie database. Each actor row has a foreign key called FAVORITE_ACTOR that points to the id of the Actor's favorite Actor.Technical Overview
A foreign key is a type of constraint that can be defined for a database. When defined, it helps the database to enforce referential integrity between tables. A foreign key must be defined as a primary key in the parent table. A table can contain a foreign key to itself. A foreign key must exist in the parent table or be null.Parent & Child
A table with a foreign key is often referred to as a child with the related table being called a parent. The parent/child terminology is a little misleading as the relationship between tables isn't necessarily a hierarchy.Recursive Foreign Key
A foreign key in a table that refers to itself is known as a recursive foreign key.Overview: Foreign Key | ||
Type | ||
Definition (1) | A field or set of fields that are used to establish relationships between rows in a database. | |
Definition (2) | A constraint that indicates a column or set of columns in a table is a reference to the primary key of another table or a self-reference to the table's own primary key. | |
Value | Establishing relationships between data entities.Enforcing referential integrity in a database. | |
Applies to | SQLRelational Databases | |
Related Concepts |