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
Cassandra Deep Dive: Wide-Column Model, Tunable Consistency and the Write Path
Cassandra is built for write-heavy workloads at massive scale — but only if you model data for your queries, not your intuition. This post explains the model and the write path.
Redis Deep Dive: Data Structures, Persistence, Pub/Sub and Production Patterns
Redis is not just a cache. Its five core data structures solve problems that would require complex code otherwise. This post covers all of them plus persistence and Pub/Sub.
MongoDB Deep Dive: Document Model, Aggregation Pipeline and Index Strategies
MongoDB is easy to start and hard to optimize. This post covers document schema design, the full aggregation pipeline, index types, and the patterns that separate fast from slow.
Comments