In database management systems, a Foreign Key is an attribute (or set of attributes) in one table that refers to the primary key (or a candidate key) of another table. It is the main mechanism in the relational model for creating and enforcing relationships between tables. Foreign keys ensure that links between records remain consistent and valid, which is called referential integrity.

Foreign keys are used extensively in relational databases to represent one‑to‑many and many‑to‑many relationships. They are the backbone of table joins and relational design.

What is a Foreign Key?

A Foreign Key is:

  • An attribute (or combination of attributes) in a child table.

  • Whose values must match the values of a primary key or unique key in a parent table.

  • Used to link rows in different tables (for example, an employee record linking to a department record).

A foreign key constraint ensures that:

  • You cannot insert a row with a foreign‑key value that does not exist in the referenced table.

  • You cannot delete a parent row that is still referenced by foreign keys, unless the database supports actions like CASCADE or SET NULL.

Example with a Foreign Key

Consider two tables: DEPARTMENT and EMPLOYEE.

DEPARTMENT Table:

Dept_IDDept_NameLocation
D101HRKolkata
D102ITPatna

EMPLOYEE Table:

Emp_IDNameDept_IDSalary
E1AmanD10150000
E2RiyaD10260000

Here:

  • Dept_ID in DEPARTMENT is the primary key.

  • Dept_ID in EMPLOYEE is the foreign key that references Dept_ID in DEPARTMENT.

In SQL, this is defined as:

sql
ALTER TABLE EMPLOYEE ADD CONSTRAINT fk_dept FOREIGN KEY (Dept_ID) REFERENCES DEPARTMENT(Dept_ID);

This foreign key ensures that every employee is assigned to a valid department and prevents inserting an employee with a Dept_ID like D999 if that department does not exist.

Key Points about Foreign Keys

  • A foreign key maintains referential integrity between tables.

  • The referenced column is usually a primary key or unique key in the parent table.

  • Foreign keys enable JOIN operations between tables.

  • They support cascading actions such as ON DELETE CASCADE or ON UPDATE CASCADE to automatically update or delete related records.

  • A table can have multiple foreign keys pointing to different parent tables.

Why Foreign Keys Matter?

Foreign keys are essential because:

  • They enforce data consistency across related tables.

  • They prevent orphan records (child records without a valid parent).

  • They make relationships between entities visible and easy to query.

  • They are the foundation of normalized relational design and entity‑relationship modeling.

Summary

A Foreign Key in DBMS is an attribute (or set of attributes) in one table that references the primary key of another table. It is used to link tables and maintain referential integrity, ensuring that relationships between records remain valid at all times. Foreign keys are central to relational databases and are crucial for building accurate, connected data models.