Databases & SQL · reviewed in July 2026
Foreign key
A foreign key is a column in one table that points to another table's primary key, establishing a relationship between the two. It's the mechanism relational databases use to connect, for example, each order with the customer who placed it.
Frequently asked questions
What happens if I try to insert a foreign key value that doesn't exist in the referenced table?
The database rejects the operation with a referential integrity constraint error — it won't let a row point at something that doesn't exist.
Can a foreign key be NULL?
Yes, unless the column is explicitly declared as required (NOT NULL). A NULL value means that row, for now, isn't related to any row in the other table.
What happens if I delete the referenced row?
It depends on the configured ON DELETE rule: it can block the delete, cascade-delete the related rows, or set the foreign key to NULL.