- Published on
Keys in Relational Databases - Types and Examples Explained
- 🔐 Introduction to Keys in Relational Databases
- 🧩 Types of Keys in DBMS
- 🧾 Summary Table of Key Types
- ✅ Final Thoughts
🔐 Introduction to Keys in Relational Databases
In relational databases, keys are essential for keeping data unique, connected, and consistent. They help uniquely identify records, build relationships between tables, and ensure that data stays accurate and reliable.
This guide will break down all the key types in simple terms—with clear examples.
🧩 Types of Keys in DBMS
1. Primary Key
- A primary key uniquely identifies each row in a table.
- Must be unique and cannot be null.
✅ Example: In a Student
table, Roll Number
is a primary key because it uniquely identifies each student.
2. Candidate Key
- A candidate key is any field (or group of fields) that can be a primary key.
- A table can have multiple candidate keys, but only one becomes the actual primary key.
✅ Example: In the Student
table, both Roll Number
and Email
(if unique) are candidate keys.
3. Alternate Key
- An alternate key is a candidate key that was not chosen as the primary key.
- It still uniquely identifies records.
✅ Example: If Roll Number
is the primary key, then Email
is an alternate key.
4. Foreign Key
- A foreign key links one table to another.
- It refers to the primary key in another table and helps enforce referential integrity.
✅ Example: In the Student
table, CourseID
can be a foreign key that refers to the Course
table’s primary key.
5. Composite Key
- A composite key uses two or more attributes together to uniquely identify a row.
- It’s used when no single field is unique on its own.
✅ Example: In an Enrollment
table, the combination of StudentID
and CourseID
can be a composite key.
6. Super Key
- A super key is any combination of attributes that uniquely identifies a row.
- It may include extra attributes not needed for uniqueness.
✅ Example: {Roll Number, Name}
is a super key if Roll Number
alone is unique.
🧾 Summary Table of Key Types
Key Type | Purpose | Example |
---|---|---|
Primary Key | Uniquely identifies each record in a table | Roll Number in Student |
Candidate Key | Possible unique identifiers | Roll Number, Email |
Alternate Key | Candidate key not chosen as primary | Email if Roll Number is primary |
Foreign Key | Connects two tables | CourseID in Student |
Composite Key | Combines multiple fields for uniqueness | StudentID + CourseID |
Super Key | Any set of attributes ensuring uniqueness | {Roll Number, Name} |
✅ Final Thoughts
Keys are critical for structuring relational databases correctly. They help enforce:
- Uniqueness
- Relationships
- Data integrity
In the next post, we’ll explore constraints and how they further strengthen relational databases by applying rules on data values.