The FOREIGN KEY constraint establishes a relationship between two tables by requiring that values in the child (referencing) column(s) match existing values in the parent (referenced) table’s primary or unique key column(s). When applied, the database checks every INSERT, UPDATE, or DELETE on the child or parent table to maintain referential integrity. Optional ON DELETE and ON UPDATE clauses let you specify actions such as CASCADE, RESTRICT, SET NULL, or NO ACTION when the parent row is modified. A child column must share the same data type and size as the referenced column, and the parent column must be indexed as a PRIMARY KEY or UNIQUE. Some engines (e.g., MyISAM in MySQL) ignore foreign keys, while others require explicit indexes. Deferrable constraints, supported in PostgreSQL and Oracle, let you delay enforcement until COMMIT. A foreign key can reference the same table (self-reference) or multiple columns (composite key).
child_column
(s) - column list - Column(s) in the child table that hold the foreign key values.parent_table
(identifier) - Table that contains the referenced primary or unique key.parent_column
(s) - column list - Column(s) in the parent table being referenced.action
(keyword) - Optional referential action|||CASCADE, SET NULL, SET DEFAULT, RESTRICT, or NO ACTION.DEFERRABLE
(NOT DEFERRABLE) - keyword|||Optional clause to delay enforcement until commit (if supported).PRIMARY KEY, REFERENCES, UNIQUE, ON DELETE, ON UPDATE, CHECK, DEFERRABLE
SQL-92 standard
The INSERT fails with an integrity constraint violation. The database rejects the row to protect referential integrity.
Yes. A table can define multiple foreign key constraints pointing to different parent tables or even to the same table.
Use ON DELETE RESTRICT or NO ACTION, or first update/delete the child rows explicitly, then remove the parent row.
Minimal overhead when properly indexed. They often improve query plans because the optimizer knows the relationship between tables.