Keys in DBMS
Keys are attributes or a set of attributes used to uniquely identify
records (tuples) in a database table.
They play a vital role in maintaining data integrity and relationships.
1. What is a Key?
A key is an attribute or combination of attributes that uniquely identifies a tuple in a relation.
- Ensures uniqueness
- Helps establish relationships
- Improves data integrity
2. Types of Keys in DBMS
- Super Key
- Candidate Key
- Primary Key
- Alternate Key
- Composite Key
- Foreign Key
3. Super Key
A super key is a set of one or more attributes that can uniquely identify a record in a table.
STUDENT (RollNo, Email, Name) Super Keys: - RollNo - Email - (RollNo, Name)
4. Candidate Key
A candidate key is a minimal super key. It has no unnecessary attributes.
Candidate Keys: - RollNo - Email
- Minimal and unique
- One candidate key is selected as primary key
5. Primary Key
A primary key is a candidate key chosen to uniquely identify each record in a table.
- Cannot be NULL
- Must be unique
- Only one primary key per table
STUDENT RollNo → Primary Key
6. Alternate Key
Alternate keys are candidate keys that are not selected as the primary key.
Candidate Keys: RollNo, Email Primary Key: RollNo Alternate Key: Email
7. Composite Key
A composite key consists of two or more attributes used together to uniquely identify a record.
ENROLL (RollNo, CourseID) Composite Key → (RollNo, CourseID)
8. Foreign Key
A foreign key is an attribute in one table that refers to the primary key of another table.
STUDENT (RollNo) ← Primary Key ENROLL (RollNo) ← Foreign Key
- Maintains referential integrity
- Prevents invalid references
9. Comparison of Keys
Key Type Purpose -------------- -------------------------------------- Super Key Identifies records (may be non-minimal) Candidate Key Minimal unique identifier Primary Key Selected main identifier Alternate Key Candidate key not chosen as primary Composite Key Combination of attributes Foreign Key Links tables together
10. Importance of Keys in DBMS
- Ensure data uniqueness
- Maintain data integrity
- Enable relationships between tables
- Improve query performance
Practice Questions
- What is a key in DBMS?
- Explain super key and candidate key.
- What is a primary key?
- Define foreign key with example.
- Differentiate between primary key and candidate key.
Practice Task
Design tables for a college database and identify:
✔ Primary keys
✔ Foreign keys
✔ Composite keys
✔ Alternate keys