Master relational database basics with this guide to primary and foreign keys in SQL.
When you're learning SQL and relational databases, two terms come up early and often: primary keys and foreign keys. These concepts are central to how relational databases structure and connect data.
In this guide, we’ll explain what primary and foreign keys are, why they matter, how to use them properly, and some common mistakes to avoid. Whether you're designing your first database schema or debugging a JOIN that doesn’t work, this article will give you a foundational understanding of how keys maintain integrity and establish relationships between tables.
A primary key is a column or set of columns that uniquely identifies each row in a table. Every table in a well-designed database should have a primary key.
Imagine a users
table:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(255)
);
Here, id
is the primary key. Every user has a unique id
, and this value identifies each record in the table.
A foreign key is a column (or set of columns) in one table that references the primary key in another table. Foreign keys create relationships between tables and help enforce referential integrity, which means the database ensures that relationships between tables remain valid.
Now, let’s say we have an orders
table that stores orders made by users:
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
order_date DATE,
total_amount DECIMAL(10, 2)
);
Here, user_id
is a foreign key that references the id
column in the users
table. This ensures that every order is tied to a valid user.
Understanding and using keys properly is crucial for:
Foreign keys prevent situations where a record refers to a non-existent row. For example, you can’t insert an order for a user_id
that doesn’t exist.
Keys allow you to split data into multiple related tables, reducing redundancy and improving maintainability.
Relational databases are optimized to JOIN tables based on key relationships, which improves performance and clarity.
Foreign keys can define what happens when a related record is updated or deleted. For example:
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
This will automatically delete orders when the related user is deleted.
There are several ways to define primary and foreign keys in SQL.
-- Inline
id SERIAL PRIMARY KEY
-- Table-level
CREATE TABLE products (
product_id INT,
name VARCHAR(100),
PRIMARY KEY (product_id)
);
-- Inline
user_id INTEGER REFERENCES users(id)
-- Table-level
CREATE TABLE posts (
post_id INT PRIMARY KEY,
author_id INT,
FOREIGN KEY (author_id) REFERENCES users(id)
);
Sometimes a single column isn't enough to uniquely identify a row. In that case, you can use composite keys, which are a combination of two or more columns.
CREATE TABLE enrollments (
student_id INT,
course_id INT,
enrollment_date DATE,
PRIMARY KEY (student_id, course_id)
);
In this case, the combination of student_id
and course_id
ensures uniqueness.
Foreign keys can also reference composite primary keys:
FOREIGN KEY (student_id, course_id) REFERENCES enrollments(student_id, course_id)
users(id)
→ primary keyorders(user_id)
→ foreign keyorder_items(order_id)
→ foreign keyThis structure ensures that every order belongs to a user, and every item belongs to a valid order.
authors(author_id)
→ primary keyposts(author_id)
→ foreign keycomments(post_id)
→ foreign keyThis allows queries like:
SELECT * FROM posts WHERE author_id = 5;
Or to join posts with authors:
SELECT p.title, a.name FROM posts p JOIN authors a ON p.author_id = a.author_id;
Every table should have a primary key. Without one, your data may be inconsistent, and you’ll run into trouble with JOINs.
If your primary key isn’t truly unique or allows NULLs, the table structure is flawed.
While most databases index primary keys by default, foreign keys are not always indexed. Indexing them improves JOIN performance.
Avoid using arbitrary combinations like first_name + last_name
as a primary key. Use a synthetic key (like id
) for simplicity.
SERIAL
or AUTO_INCREMENT
(MySQL) for primary key columns.user
, order
) or plural (users
, orders
) consistently.Primary and foreign keys are the backbone of relational databases. They ensure that your data is reliable, consistent, and connected in meaningful ways. Understanding how to use keys effectively is essential for anyone working with SQL.
By applying the principles outlined here, you’ll be able to build schemas that scale, support efficient queries, and prevent data anomalies.
Want to try building key relationships in a real SQL workspace? Get started with Galaxy — an AI-powered SQL editor designed for fast querying and schema exploration.
Continue learning: