What is a foreign key (FOREIGN KEY)?
A foreign key (FOREIGN KEY) is a field or combination of fields in one table that references the primary key of another table, creating a link between the tables.
The main points
- A link between tables: a foreign key shows that a record in one table is connected to a record in another table.
- Supporting data integrity: the database checks that the foreign key's value matches an existing value of the other table's primary key.
- Example:
- The
Employeestable has aDepartmentIDfield (a foreign key) that references theIDfield in theDepartmentstable. - That means every employee is tied to an existing department.
- Updates and deletes: when a record in the parent table changes or is deleted, the behavior can be configured, blocking the delete, or cascading the delete or update.
Put simply, a foreign key is a "reference" to another table, linking records together and keeping data consistent across tables.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.