Foreign keys put the “relational” in “relational database” – they help define the relationship between tables. They allow developers to maintain referential integrity across their database. Foreign keys also help end-users by preventing errors and improving the performance of any operation that’s pulling data from tables linked by indexed foreign keys. Show
On a technical level, a foreign key is a constraint that links a column in one table ( For example, a table of customer orders might have a How do foreign keys actually work in practice? Let’s get practical, and learn more about foreign keys by looking at how they function in the context of a simple SQL database. In this article, we’ll cover:
Let’s dive in! Our example databaseBelow, we’ve set up a sample database we’ll work with. It represents the sales database of a fictional online bookshop. We can see there are three tables:
Needless to say, the database of a real-life bookshop would be far larger and more complex than this! But this sample database will make it easier to illustrate how foreign keys work, and the principles that apply here will apply in exactly the same way in larger and more complex databases. (If you’d like to try working with this database hands-on, skip to the end of the article for instructions that’ll help you get it set up on a free CockroachDB dedicated cluster in less than five minutes). What’s the difference between primary keys and foreign keys?To understand how foreign keys work, it will help to first understand primary keys. What is a Primary Key?A primary key is a column in a table that is used as a unique identifier for each row. It functions a bit like the row’s address, and is used for the table’s primary index. (Technically, a primary key can be made up of multiple columns, but for our purposes here let’s think of it as a single column). You can think of the primary key as being like a row’s ID number. Just as your government ID identifies you uniquely even if other people share your name or live at your address, a table’s primary key column identifies each row uniquely even if some of the other values in the row are shared with other rows. A table’s primary key column thus must be unique, and it cannot be empty or Consider the In fact, in this table Here’s how our database looks with the primary key columns for each table highlighted:
Foreign KeysForeign keys link data in one table to the data in another table. A foreign key column in a table points to a column with unique values in another table (often the primary key column) to create a way of cross-referencing the two tables. If a column is assigned a foreign key, each row of that column must contain a value that exists in the ‘foreign’ column it references. The referenced (i.e. “foreign”) column must contain only unique values – often it is the primary key of its table. For a tangible example, let’s look at the When we’re setting up this table, it would make sense to add foreign key rules to both
Using these foreign keys saves us from having to store the same data repeatedly – we don’t have to store the user’s name in the But the real purpose of foreign keys is that they add a restriction: entries to the table with a foreign key must have a value that corresponds with the ‘foreign’ table column. This restriction is called a foreign key constraint. Let’s take a look at foreign key constraints in more detail. What is a foreign key constraint?Foreign key constraints are the rules created when we add foreign keys to a table. Foreign key
constraints in table (Note: foreign keys can be composite keys, so the foreign key for one column could be two or more columns in another table. In this article, for the sake of simplicity we’ll focus on linking a single column in one table to a single column in another.) For example, imagine we’ve set up our 1. Any value entered into |