Skip to content
Databasesbeginner

What is a foreign key?

Updated Nov 30, 20263 min readBased on official documentation
What is a foreign key?

In short

A foreign key is a column, or group of columns, in one table that points to a row in another table. It links related data together and lets the database reject references to rows that do not exist. Foreign keys help keep data consistent across tables.

What a foreign key does

A database often stores related information in separate tables. An order table might store the ID of the customer who placed it, while the customer details live in a customers table. The order needs a way to say which customer it belongs to. A foreign key is the rule that makes this link.

A foreign key sits in one table, called the child or referencing table. It points to a key in another table, called the parent or referenced table. Once the rule exists, the database checks each value you store in the foreign key column. If the value does not match a row in the parent table, the database refuses to store it and raises an error.

This check is useful. Without it, you could record an order for customer number 999 even though no such customer exists. The data would look complete but would be wrong.

How it works

The PostgreSQL documentation describes a family of rules called constraints. A constraint limits what values a column can hold, and if a user tries to break it, the database raises an error. A foreign key is one of these constraints, along with checks, not-null rules and unique rules.

A foreign key usually refers to a column that is marked as unique in the parent table, most often its primary key. A primary key is the column, or group of columns, that identifies each row. Because the parent value is unique, each child row points to exactly one parent row.

Here is a small example with two tables:

CREATE TABLE customers (
    customer_id integer PRIMARY KEY,
    name text NOT NULL
);

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    customer_id integer REFERENCES customers (customer_id),
    total numeric CHECK (total > 0)
);

The line REFERENCES customers (customer_id) creates the foreign key. Inserting an order with a customer_id that is not in the customers table will fail.

When to use it

Use a foreign key whenever one table depends on another. Typical cases include orders and customers, comments and posts, or line items and invoices. If you find yourself storing an ID that should always match a row somewhere else, a foreign key is the right tool.

The documentation also notes that a check constraint should not look at other tables. It recommends using unique, exclusion or foreign key constraints for rules that span rows or tables. So when you need a rule between tables, a foreign key is usually the safe choice.

Common mistakes

A frequent mistake is to forget that a foreign key needs a matching value first. You must insert the parent row before the child row, or the insert will fail. Plan the order of your data loads with this in mind.

Another mistake is to treat null as a valid match. A foreign key column can hold null in many designs, which means the row has no parent yet. If every row must have a parent, add a not-null constraint to the column as well. The documentation explains that a not-null constraint is the clear way to forbid null values, since a check constraint alone does not block them.

Finally, beginners sometimes skip naming their constraints. The documentation says a named constraint makes error messages easier to read and lets you refer to the constraint when you change it later. If you do not name it, the system picks a name for you.

Alternatives

You can skip a foreign key and check references in your application code. This is slower to write and easy to get wrong, because any other program that writes to the table can skip your checks. A database-level foreign key protects the data no matter which program writes it.

If you are new to tables and columns, read our introduction to relational databases first, then come back to this rule. Foreign keys make the most sense once you see how primary keys identify rows.

FAQ

Does a foreign key slow down inserts?

It can add a small amount of work, because the database must look up the parent row each time. For most applications the safety it gives is worth that cost.

Can a foreign key point to a column in the same table?

Yes. A table can reference itself, which is useful for structures such as an employee table where each employee has a manager who is also an employee.

Can a foreign key be empty?

By default a foreign key column can hold null, meaning there is no link yet. Add a not-null constraint if every row must have a parent.

What happens if I try to delete a parent row that children still use?

The database normally refuses the delete, so the child rows never point to missing data. Some setups allow related child rows to be removed automatically instead.

Sources

This page is based on the official documentation below. Read it for full detail.

Foreign key — official documentation ↗

Foreign key in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.