A foreign key is a relational database concept that establishes a link or relationship between two tables based on a field or set of fields. It represents a column or combination of columns in one table that refers to the primary key (or a unique key) in another table. The purpose of foreign keys is to enforce referential integrity between related tables.
Key points about foreign keys:
- Definition: A foreign key in one table points to a primary key or unique key in another table, creating a parent-child relationship between the tables.
- Usage: It ensures that values in the referencing column (foreign key column) of the child table must exist in the referenced column (primary key or unique key column) of the parent table.
- Referential Integrity: Foreign keys help maintain data consistency and integrity by preventing actions that would orphan records (such as deleting a parent record that has dependent child records).
- Syntax: In SQL, foreign keys are defined using the
FOREIGN KEY
constraint to establish and enforce these relationships.