0% completed
In relational databases, keys play a crucial role in uniquely identifying records within a table, establishing relationships between tables, and maintaining data integrity. Keys ensure that each record in a database can be accurately retrieved, updated, or deleted without ambiguity. This lesson covers the different types of keys used in relational databases.
A primary key is a unique identifier for each record in a table. It ensures that each row in a table is distinct, meaning no duplicate values are allowed in the primary key column(s). Primary keys cannot contain null values.
A candidate key is any attribute, or a combination of attributes, that can uniquely identify a row in a table. A table can have multiple candidate keys, but only one of them can be chosen as the primary key.
An alternate key is a candidate key that is not selected as the primary key. It serves as an alternative way to uniquely identify records within the table.
A foreign key is an attribute in one table that links to the primary key of another table. Foreign keys establish relationships between tables, enforcing referential integrity by ensuring that values in the foreign key column must match values in the referenced primary key column.
A composite key is a key that consists of two or more attributes used together to uniquely identify a record. Composite keys are often used when no single attribute can uniquely identify a row.
A super key is any combination of attributes that can uniquely identify a row in a table. Super keys include all candidate keys, primary keys, and any additional attributes that make them unique. Super keys may contain more attributes than necessary to ensure uniqueness.
Key Type | Purpose | Example |
---|---|---|
Primary Key | Uniquely identifies each record in a table | Roll Number in Student |
Candidate Key | Potential attributes for unique identification | Roll Number, Email |
Alternate Key | Candidate key not chosen as primary key | Email if Roll Number is primary |
Foreign Key | Establishes a link between two tables | Course ID in Student |
Composite Key | Combines multiple attributes to ensure uniqueness | Student ID and Course ID |
Super Key | Any set of attributes that uniquely identifies a row | {Roll Number, Name} |
.....
.....
.....