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.

2. Types of Keys in DBMS

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

5. Primary Key

A primary key is a candidate key chosen to uniquely identify each record in a 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

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

Practice Questions

  1. What is a key in DBMS?
  2. Explain super key and candidate key.
  3. What is a primary key?
  4. Define foreign key with example.
  5. 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