Database Design Fundamentals: Primary Keys, Foreign Keys, and Constraints Explained
Introduction
Welcome back to the Database Design Fundamentals series!
In the previous article, we explored entities, tables, and relationships — the building blocks of any database. Now, we’ll take the next step: understanding primary keys, foreign keys, and constraints.
These elements are the rules of the game. They ensure your database remains accurate, consistent, and reliable. By the end of this post, you’ll know what each of these terms means and how they work together.
What Is a Primary Key?
A primary key is a column (or set of columns) that uniquely identifies each row in a table.
Example: In a
Studentstable:StudentIDcould be the primary key.
Rules:
Must be unique (no duplicates).
Cannot be NULL (every row must have a value).
👉 Think of it like a passport number — no two people can share the same one.
What Is a Foreign Key?
A foreign key is a column in one table that points to a primary key in another table.
Example:
In an
Enrollmentstable:StudentID→ referencesStudents(StudentID)CourseID→ referencesCourses(CourseID)
This creates a relationship between tables.
👉 Think of it like a reference in a library — it tells you where to find the related record.
What Are Constraints?
Constraints are rules enforced by the database to keep data valid.
Common Types of Constraints
PRIMARY KEY Constraint
Ensures uniqueness and non-null values.
FOREIGN KEY Constraint
Ensures values match an existing row in the referenced table.
UNIQUE Constraint
No duplicate values in a column.
Example:
Emailin aUserstable.
NOT NULL Constraint
Column cannot be empty.
Example: Every student must have a
Name.
CHECK Constraint
Adds a condition for valid data.
Example:
Age >= 0for aStudentstable.
Why Do They Matter?
Without keys and constraints, a database would quickly fall apart:
Duplicate rows → two students with the same ID.
Broken relationships → an enrollment pointing to a non-existent course.
Invalid data → negative ages or empty emails.
Keys and constraints are the guardrails that keep your data clean and trustworthy.
Real-World Analogy
Imagine a university registration system:
Primary Key: Each student has a unique student ID.
Foreign Key: Each course enrollment points to a valid student and course.
Constraints:
You can’t enroll in a course that doesn’t exist.
A student’s date of birth can’t be in the future.
Without these rules, the system would be chaotic — duplicate students, fake enrollments, and nonsense data everywhere.
Conclusion & Next Steps
Primary keys, foreign keys, and constraints form the rules of structure and consistency in databases. They ensure accuracy and prevent invalid or duplicate data.
In the next post, we’ll explore Normalization — specifically 1NF, 2NF, and 3NF — and how it helps organize your data for efficiency and reliability.
Related
Part 3.6 — Migrations, Environments & Local Dev Workflows
A productive backend engineer knows how to manage migrations, environments, Dockerized databases, and workflow scripts. This post makes those skills second nature.
Database Design Fundamentals: Schema Evolution and Migrations Explained
Learn how databases evolve with schema changes. Understand migrations, versioning, and best practices for adapting databases over time.
Database Design Fundamentals: Many-to-Many Relationships and Join Tables
Understand many-to-many relationships in databases and how join tables solve them with clear examples and best practices.
Comments