What Is a Foreign Key? Explained Simply
The link that connects one database table to another.
What a foreign key is
A foreign key is a column in one database table that refers to the primary key of another table, creating a link between them. If the primary key uniquely identifies records in its own table, a foreign key in a different table points to those records. This is the mechanism that connects related data across a relational database. Foreign keys are what turn a collection of separate tables into an interconnected, relational whole.
Creating relationships
Foreign keys are how relationships between tables are made concrete. Consider a customers table and an orders table. Each order needs to know which customer placed it. Instead of copying the customer's details into every order, the orders table stores a foreign key, the customer's primary key value, that points to the right row in the customers table. This neatly links each order to its customer while keeping the customer's information in just one place.
Why not just repeat data?
Without foreign keys, you might be tempted to copy related information into every record, storing the full customer details on every order, for example. But that wastes space and, worse, creates inconsistency: if a customer's address changes, you would have to update it everywhere it was copied, and any missed copy becomes wrong. Foreign keys avoid this by keeping each piece of data in one place and linking to it, which keeps the whole database consistent.
Protecting data integrity
Foreign keys do more than link data; they help protect its integrity. A database can enforce that a foreign key must point to an actual, existing record in the other table. This prevents 'orphaned' records, like an order that refers to a customer who does not exist. The database can also control what happens if a referenced record is deleted. These safeguards, called referential integrity, are an important benefit of using foreign keys properly.
Foreign and primary keys together
Primary keys and foreign keys work as a pair. The primary key uniquely identifies each record in its home table. The foreign key, in another table, references that primary key to establish a relationship. Together they are the foundation of how relational databases connect data: tables stay focused on one kind of thing, and foreign keys weave them into a coherent, interconnected structure that can answer complex questions spanning multiple tables.
Why it matters
Foreign keys are essential to how relational databases link related information and keep it consistent. Understanding them, and how they work with primary keys, clarifies how databases avoid duplicated data, connect tables, and protect against errors. For anyone learning about databases, foreign keys are a key concept that explains how structured data stays both organized and reliably interconnected across many tables.
Related on Skillo
See also: What is a primary key? Explained simply, What is a relational database? Explained.
Sources
Published date reflects the original event date (2024-05-28). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.
Written by
Skillo Staff
0 Comments
Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.