Referential integrity: prevent orphaned database records
Learn how foreign keys keep references valid, how optional and composite links work, what to decide when a parent changes, and how to add a constraint to existing data.
Referential integrity: prevent orphaned database records
Referential integrity means keeping links between tables valid. If an order points to a customer ID that does not exist, the order is orphaned. A relational database can enforce the rule with a foreign key: it rejects a reference to a missing parent and applies a defined policy when the referenced row changes or is deleted.
Parent and child rows
The table whose row is referenced is the parent (referenced) table; the table holding the foreign key is the child (referencing) table. For example, orders.customer_id can reference customers.customer_id:
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,
ordered_at timestamptz NOT NULL,
CONSTRAINT orders_customer_fk
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);In this PostgreSQL example, a customer row must exist before an order can refer to it. An application lookup can race with a concurrent deletion; the database constraint protects consistency at write time. Referenced columns need a suitable primary or unique key, and the referencing and referenced column counts and types must match.
Optional and composite references
If customer_id is mandatory, declare NOT NULL as shown. An optional link can use a nullable foreign key. With PostgreSQL's default MATCH SIMPLE, if any column in a composite foreign key is NULL, no matching parent row is required. If all columns must be present together or all absent, check whether MATCH FULL fits and verify support in the target DBMS. Components of a required composite reference are commonly also NOT NULL.
When a child refers to a composite parent key, match the number, order, and types of the columns. Use separate foreign keys or a composite one according to the actual key the relationship must reference.
Decide what happens when a parent changes
You must choose what happens if a referenced customer ID changes or the customer is deleted. The database can reject the operation, block it while children refer to the parent, set child values to NULL or a default, or cascade the deletion to child rows. The right policy depends on whether the records are independent or the child is a component that cannot exist without the parent. CASCADE is one lifecycle policy, not a synonym for referential integrity. Check retention, audit, and recovery needs before choosing it.
Add a foreign key to existing data
An orphan is a child value with no matching parent. Existing orphans can make a new constraint fail. Define NULL policy, then find child rows with no parent. This PostgreSQL query illustrates the check:
SELECT o.order_id, o.customer_id
FROM orders AS o
LEFT JOIN customers AS c ON c.customer_id = o.customer_id
WHERE o.customer_id IS NOT NULL
AND c.customer_id IS NULL;Review each result with the data owner, restore the correct relationship or agree how to preserve the record, then add the constraint. Deleting rows or inventing parent IDs can hide the problem and damage references. Control write paths so new orphans do not appear during cleanup. Check the target DBMS version's supported validation and locking behavior. After applying the rule, verify that bad references are rejected while valid links and intended optional NULLs work.
Index and consistency considerations
Foreign-key checks need indexes to locate matching keys efficiently. Without an index on child reference columns, joins or checks triggered by parent changes can be costly. Products differ in whether they create or require indexes automatically. In PostgreSQL, do not assume a foreign-key declaration automatically creates an index on the referencing columns. Choose indexes based on lookup patterns and parent-update costs.
Choosing the rule
Every child value must identify an existing parent: declare a foreign key and, if required, `NOT NULL`.
A link is optional: allow NULL and verify composite-key NULL semantics for the DBMS.
Parent updates or deletes: choose `RESTRICT`/`NO ACTION`, `SET NULL`, `SET DEFAULT`, or `CASCADE` to match the data lifecycle.
Existing table: find and resolve orphans first, then apply and verify the constraint.
Sources and scope
[PostgreSQL 18: Constraints — foreign keys and referential actions](https://www.postgresql.org/docs/18/ddl-constraints.html)
[MySQL 8.4: Foreign Key Constraints](https://dev.mysql.com/doc/refman/8.4/en/create-table-foreign-keys.html)
Documentation checked 2026-11-23. SQL is illustrative and was not executed. Foreign-key options, NULL matching, and index requirements vary by DBMS and version.