RelationalDBDesign
RedhatLinuxSys Seomining
prev next prev next
Course navigation
Lesson 14 Database Foreign keys
Objective Define foreign keys for Relational Database.
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.
Customer 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 A contains a field that is the primary-key field in table B, that field in table A is referred to as a foreign key .
Foreign key: A field (or combination of fields) used to link tables; a corresponding primary key field occurs in the same database.
Foreign Key - Primary Key
A foreign key field can be identified with the primary key of more than one table.
This is not a requirement.
Benefits of Foreign Keys
While foreign keys don't uniquely identify records in their tables, they do provide an important benefit. They enable you to create links between tables that share fields. Again, consider the CUSTOMER and ORDER tables.
Customer ID Primary Key
The line between the CustID fields in the two tables represents the link between the two tables. Linking tables enables you to join the tables and combine the records that share values in the linked fields.
Primary Key Field
A primary-key field and a foreign-key field don't need to have the same name to be linkable.
They do need to contain the same data. Remember, the RDBMS doesn't identify the link between the tables, the designer 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
Course navigation