In relational databases, keys are the foundation for ensuring data integrity and establishing relationships between tables. They help uniquely identify records, enforce constraints, and connect data across different tables.Understanding their roles will help in designing efficient, reliable databases.
Lets explore the different types of keys using a simple example with three tables: Employees, Departments, and Projects.
1. Super Key : The Foundation of Uniqueness
A Super Key is any set of columns that can uniquely identify a row in a table. It doesn’t have to be minimal (i.e., it can include unnecessary columns).
Let’s start with the Employees table:
Super Keys in the Employees table:
{EmployeeID}
{EmployeeID, Name}
{EmployeeID, Phone, Email}
{Email}
{Phone, Email}
Key Takeaway: A Super Key includes any combination of columns that uniquely identifies a row.
2. Candidate Key : Potential Primary Keys
A Candidate Key is any minimal Super Key that could serve as a Primary Key.
In the Employees table, the following are Candidate Keys:
EmployeeID
Email
Both columns are minimal and can uniquely identify each employee. However, only one can be selected as the Primary Key. Here, we’ve chosen EmployeeID
.
Key Takeaway: Candidate Keys are like contenders in a competition to become the Primary Key.
3. Primary Key : The Chosen One
A Primary Key is a special Super Key that:
- Uniquely identifies each row.
- Is minimal (contains only the necessary columns).
- Cannot have
NULL
values.
In the Employees table:
EmployeeID
is the ideal Primary Key because it uniquely identifies each employee and is minimal.
Key Takeaway: A Primary Key is the most important key that uniquely identifies records.
4. Alternate Key : The Runner-Up
Once the Primary Key is chosen, the remaining Candidate Keys become Alternate Keys.
In the Employees table:
EmployeeID
is the Primary Key.Email
becomes an Alternate Key.
Key Takeaway: An Alternate Key is a backup key that wasn’t chosen as the Primary Key but is still unique.
5. Composite Key : A Key Made of Multiple Columns
A Composite Key is a Primary Key that consists of two or more columns.
Let’s look at the Projects table:
Here:
- Neither
ProjectID
norEmployeeID
alone can uniquely identify each record. - The combination of
ProjectID
andEmployeeID
forms a Composite Primary Key.
Key Takeaway: A Composite Key is used when a single column isn’t enough to uniquely identify records.
6. Surrogate Key : An Artificial Key
A Surrogate Key is an artificial key created by the system. It’s often an auto-incrementing number used when no natural key is suitable.
Let’s add a BorrowingID to the Borrowings table to uniquely identify each record:
Key Takeaway: A Surrogate Key is an artificial identifier with no business meaning.
7. Foreign Key : Linking Tables
A Foreign Key establishes a relationship between two tables by referencing the Primary Key of another table.
Departments Table
In the Employees table, the DepartmentID column is a Foreign Key referencing the Primary Key in the Departments table:
This ensures that:
- The DepartmentID in Employees must match a valid DepartmentID in Departments.
Key Takeaway: A Foreign Key enforces referential integrity between two tables.
Conclusion
Keys are like the glue holding your data together:
- They make sure every record is unique.
- They maintain accuracy and integrity.
- They create relationships between different pieces of data.
- They keep your database efficient and organized.