# Foreign Key

> A column that points to a row in another table. The link is checked for you.

**Field:** Software Development  
**Category:** Databases  
**Also known as:** FK, Foreign Key Constraint

## Think of it like

Think of it like a name tag on a lunch box that says whose it is. The tag must match a real pupil.

## Definition

A foreign key is a column that points to a row in another table. It holds that row's primary key, so an order can point to its customer. The database checks the link. You cannot point at a row that is not there. It can also stop you from deleting a row that others point to. This is how a relational database ties its tables together.

## Example

Each order has a customer_id. When Jack tries to save an order for customer 99, who does not exist, the database says no.

## Example code

```sql
INSERT INTO orders (customer_id, total) VALUES (99, 5);
-- Error: FOREIGN KEY constraint failed
```

There is no customer 99, so the order is refused.

## Why it matters

They stop orders that point at customers who are gone.

## Common mistake

Leaving them out to save time. Broken links then pile up quietly until a report adds up wrong.

## Three principles

- **Points elsewhere.** To a primary key in another table.
- **Checked for you.** No links to rows that do not exist.
- **Ties tables together.** The heart of a relational database.

## Related terms

- [Primary Key](https://learn.benibix.com/term/development/primary-key)
- [SQL Join](https://learn.benibix.com/term/development/join)
- [Relational Database](https://learn.benibix.com/term/development/relational-database)
- [Database Table](https://learn.benibix.com/term/development/database-table)
- [One-to-Many Relationship](https://learn.benibix.com/term/development/one-to-many-relationship)

## Told apart from

- [Primary Key vs Foreign Key](https://learn.benibix.com/compare/primary-key-vs-foreign-key): A primary key names each row in its own table. A foreign key sits in another table and points back to that name.

---

Source: https://learn.benibix.com/term/development/foreign-key
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
