What Is a Database Foreign Key? Keeping Table References Valid
A foreign key is a database constraint that requires values in one table to refer to existing key values in another table. For example, it can prevent an order’s `customer_id` from pointing to a customer that does not exist, preserving referential integrity. If the referencing column allows NULL, a row with no value can normally bypass the reference check, so design whether the relationship is required or optional.
Why use a foreign key?
Even if an application screen lets a user choose a customer, another code path, import job, or admin tool might store an unknown ID. A foreign key checks every write that reaches the database. The referenced table is often called the parent, and the table containing the foreign key the child or referencing table.
CREATE TABLE customers (
customer_id bigint PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE orders (
order_id bigint PRIMARY KEY,
customer_id bigint NOT NULL
REFERENCES customers(customer_id),
order_date date NOT NULL
);This definition requires orders.customer_id to exist in customers.customer_id; an order with a nonexistent customer ID is rejected. Referencing and target columns must have compatible types, and the target must be unique, for example through a primary key or unique constraint. The SQL is illustrative and was not executed.
How does NULL express an optional relationship?
When a foreign-key column allows NULL, non-NULL values must match the referenced table, but NULL normally is not checked as a reference. If every order must have a customer, declare NOT NULL as well. If an assignment is optional—such as a ticket with no agent yet—NULL may be appropriate. First decide whether “unknown” and “no relationship” mean the same thing in the business domain.
A composite foreign key refers to a parent row using multiple columns. In PostgreSQL’s default MATCH SIMPLE mode, if any referencing column is NULL, the row need not satisfy the reference check. MATCH FULL can require either all referencing columns to be NULL or all to form a valid reference. Use NOT NULL on relevant columns if partially empty combinations are not allowed.
What happens when the parent is deleted or its key changes?
Choose referential actions based on what the data means. PostgreSQL’s default delete action prevents a deletion that would leave broken references. ON DELETE CASCADE automatically deletes child rows with the parent, which can fit dependent data such as order items. It can be dangerous when a parent has records that must be retained: cascading a product deletion into historical orders, for example, may destroy needed records. SET NULL or SET DEFAULT can fit optional relationships, but must agree with nullability, defaults, and other constraints.
If order items cannot exist without an order, deleting them with the order may match their lifecycle. If customer deletion must not erase order history, restricting the delete or deactivating the customer may be more appropriate. The right policy comes from retention, audit, legal, and business-lifecycle requirements, not from a database default. Consider ON UPDATE similarly when referenced key values can change.
Does a foreign key automatically create an index?
In PostgreSQL, the referenced columns must be a primary key, unique constraint, or non-partial unique index, so the referenced side has an index. Declaring a foreign key does not automatically index the child’s referencing columns. When a parent row is deleted or its key updated, the database may need to find matching child rows. An index on the foreign-key columns can help if joins, filters, or such parent changes make that lookup important. Indexes also add storage and write costs, so decide from actual access patterns.
Design and troubleshooting checks
Decide whether the relationship is required or optional, then choose NULLability for the child columns accordingly.
Verify that the columns point to the intended candidate key and that types and column order match.
Choose `ON DELETE` and `ON UPDATE` behavior from retention and lifecycle rules. Use CASCADE only after reviewing the full set of affected rows.
Before adding a constraint to an existing table, find and resolve orphaned references. Invalid existing data can cause the addition to fail.
Consider an index on referencing columns when child-row lookups during joins or parent changes need it.
When a constraint error occurs, check whether the referenced value exists and whether delete/update and NULL policies match the intended relationship.
A foreign key validates the relationship; it does not automatically index the child side, prevent duplicate references, or decide whether referenced data is correct in business terms. Those are separate concerns: query performance, uniqueness constraints, and application/domain validation.
Sources and scope
[PostgreSQL 18: Foreign Keys and referential actions](https://www.postgresql.org/docs/18/ddl-constraints.html)
[PostgreSQL 18: Table basics and keys](https://www.postgresql.org/docs/18/ddl-basics.html)
The PostgreSQL 18 documentation was checked on 2026-11-22. MATCH SIMPLE, MATCH FULL, ON DELETE/ON UPDATE, referenced-key requirements, and index behavior are described specifically for PostgreSQL. The SQL examples are illustrative and were not executed. Check other DBMS documentation for their defaults and supported syntax.