Types of Keys in DBMS (Complete Guide with Examples)

Introduction

Keys are very important in a Database Management System (DBMS). They help uniquely identify records, create relationships between tables, and maintain data integrity. Without keys, managing data accurately would be very difficult.

In this guide, you will learn the main types of keys in DBMS with simple examples.

 

What is a Key in DBMS?

A key is one or more attributes (columns) used to identify records in a table and establish relationships between tables.

Keys ensure that data in the database remains unique, consistent, and properly connected.

 

Main Types of Keys in DBMS

The most important keys are:

  1. Primary Key
  2. Foreign Key
  3. Candidate Key
  4. Super Key
  5. Alternate Key
  6. Composite Key
  7. Unique Key (optional but useful to know)

Let’s understand each one.

 

01. Primary Key

A Primary Key uniquely identifies each record in a table.

Features

  • Must be unique
  • Cannot be NULL
  • Only one primary key per table

Example

Students table

StudentID (PK)

Name

101

Ali

102

Sara

Here, StudentID is the Primary Key.

 

02. Foreign Key

A Foreign Key is a column that refers to the Primary Key of another table. It creates relationships between tables.

Features

  • Can contain duplicates
  • Can contain NULL (usually)
  • Maintains referential integrity

Example

Orders table

OrderID

StudentID (FK)

1

101

2

102

StudentID here is a Foreign Key.

 

03. Candidate Key

A Candidate Key is any column (or set of columns) that can uniquely identify a record.

From candidate keys, one is selected as the Primary Key.

Example

Students table

StudentID

NIC

Email

Possible candidate keys:

  • StudentID
  • NIC
  • Email

Any of these could uniquely identify a student.

04. Super Key

A Super Key is any set of one or more columns that can uniquely identify a record.

It may contain extra unnecessary attributes.

Example

If StudentID alone is unique, then these are super keys:

  • StudentID
  • StudentID + Name
  • StudentID + Email

All can uniquely identify the record.

 

05. Alternate Key

An Alternate Key is a candidate key that was not selected as the primary key.

Example

If:

  • StudentID → chosen as Primary Key
  • NIC → remaining candidate key

Then NIC becomes an Alternate Key.

 

06. Composite Key

A Composite Key is a primary key made of two or more columns together.

Used when a single column cannot uniquely identify records.

Example

Enrollment table

StudentID

CourseID

Grade

Here, the combination of:

  • StudentID + CourseID

forms the Composite Key.

 

07. Unique Key

A Unique Key ensures that all values in a column are unique, similar to a primary key.

Difference from Primary Key

  • Can allow one NULL value (in many DBMS)
  • A table can have multiple unique keys

Example

Email column in a Users table.

 

Summary Table

Key Type

Purpose

Primary Key

Uniquely identifies records

Foreign Key

Creates relationships

Candidate Key

Possible primary keys

Super Key

Any unique identifier set

Alternate Key

Candidate key not chosen

Composite Key

Multiple columns as key

Unique Key

Ensures column uniqueness

 

Conclusion

Keys are essential in DBMS for uniquely identifying records and maintaining relationships between tables. Understanding different types of keys helps in designing efficient and reliable databases. Every database designer and IT student must clearly understand these key concepts.




Comments

Popular posts from this blog

DBMS Normalization (1NF, 2NF, 3NF) — Easy Guide with Examples

DATA BASE MANAGEMENT SYSTEMS (DBMS)

The Importance of Learning English