What is a Foreign Key?

01 May, 2023

SQL ServerDatabaseSQL

What is a Foreign Key?

In a SQL database, a foreign key is a column, or combination of columns, in a single table that references another column in a different table. To 'reference' another column means to have the same value as the column it references.

This reference creates a relationship between the two tables, which is known as a parent-child relationship. In this relationship the parent table is the table that contains the reference column. The child table is the table that contains the column or columns that reference the parent value.

Why Use a Foreign Key?

The main purpose of using foreign keys is to maintain referential integrity in the database. The value entered into the foreign key column of the child table must exist in the parent table. If the row in the parent table that contains this reference is removed, then the foreign key cannot be entered into the child table.

This leads to a secondary use of foreign keys. They help enforce business rules in the database. A value cannot be deleted from a parent table if a child table reference is without either cascading queries or more complex business logic.

Foreign keys also make querying easier. The foreign key is traditionally also the column that tables are joined by when querying. Having a clear database structure and documentation about the foreign keys makes it much simpler to query the data.

If you liked this post, please share it!