RelationalDBDesign 




Database Analysis   «Prev  Next»
Lesson 14 Database Foreign keys
Objective Define foreign keys for a relational database.

Definition of a Foreign Key

A foreign key consists of a column that references a column (most often the primary key) of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.
When you create a database to manage information for a business, it is common for tables in the database to have columns in common. In the following illustration, the CUSTOMER and ORDER tables share the CustID column.

CustID is a primary key in the CUSTOMER table,  CustID is a foreign key in the ORDER table
CustID is a primary key in the CUSTOMER table, CustID is a foreign key in the ORDER table.
The difference between the CustID field in the CUSTOMER table and its counterpart in the ORDER table is that the CustID field is the primary-key field for the CUSTOMER table but not for the ORDER table.
When table ORDER contains a field that is the primary-key field in table CUSTOMER, that field in table ORDER is referred to as a foreign key. [1]

Benefits of Foreign Keys

While foreign keys do not uniquely identify records in their tables, they do provide an important benefit. They enable you to create links between tables that share fields.

Customer ID Primary Key
CUSTOMER and ORDER tables with a link between the foreign key (of ORDER) and primary key (of CUSTOMER).



Primary Key Field

A primary-key field and a foreign-key field do not need to have the same name to be linkable.
They do need to contain the same data. Remember, the relational database management system does not identify the link between the tables, the designer of the database tables does.
The next lesson describes referential integrity.

Relational Constructs - Quiz

Before you move on to the next lesson, click the Quiz link below to reinforce your understanding of foreign keys.
Relational Constructs - Quiz
[1] Foreign key: A field (or combination of fields) used to link tables; a corresponding primary key field occurs in the same database.