Table of contents
1.
Introduction
2.
What is the role of a Candidate Key?
3.
Properties of Candidate Key in DBMS
4.
Types of Candidate Keys
4.1.
Primary Key
4.2.
Composite Key
4.3.
Unique Key
4.4.
Alternate Key
5.
Example of Candidate Key in DBMS
6.
Why do we Need a Candidate Key?
7.
How is the Candidate Key Different from the Primary Key?
8.
Properties of Candidate Key
9.
Frequently Asked Questions
9.1.
What is primary key and candidate?
9.2.
What is a candidate key and super key?
9.3.
What is a candidate key example?
9.4.
What is a candidate key also known as?
10.
Conclusion
Last Updated: Apr 4, 2024
Easy

Candidate Key in DBMS

Author Prashansa
6 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

You must have heard about keys in DBMS. In a relational database, keys are the attributes uniquely identifying one or more rows. Keys are also used to establish relationships between two or more tables. In this article, we will discuss the Candidate Key in DBMS in detail.

candidate key in dbms

A candidate key in a relational Database refers to an attribute or set of attributes that can uniquely identify each row in a table.

There can be more than one candidate key in a table. The candidate key can be used as a primary key for the table.

Let's understand this with an example:

Consider a table called EmployeeDetails which contains details of employees in an office.

Employee ID

Employee Name

Contact Number

Address

1 Kartik Bhatia 987654321 Delhi
2 Neetesh Kumar 876543219 Mumbai
3 Kartik Singh 765432189 Delhi


Here we can see in the employee table we can uniquely define the employee by their employee id or their,  contact number. So here, the Employee id and Contact number both are candidate keys.

Also See, File System vs DBMS

What is the role of a Candidate Key?

A candidate key is a crucial concept in database design that helps ensure data integrity and uniqueness within a table. It serves as a unique identifier for each record, allowing for efficient retrieval and manipulation of data. By enforcing the presence of a candidate key, we can avoid data duplication and ambiguity, ensuring the accuracy and reliability of our database. Additionally, a candidate key forms the basis for establishing relationships with other tables, facilitating effective data normalization and maintaining the integrity of the overall database structure.

Properties of Candidate Key in DBMS

Some important properties of a Candidate Key are:

  • All the values in a candidate key are unique.
     
  • All candidate keys are super keys, but all super keys need not be candidate keys. In simpler words, we can see that the candidate key is a subset of the super key.
     
  • The candidate key can contain null value.
     
  • The candidate key should not have redundant values.
     
  • We can uniquely identify every other attribute of the table using the candidate key.
     
  • We choose the primary key from the set of candidate keys.
     
  • We can identify prime and non-prime attributes of the table using the candidate key. The attributes which make the candidate key are the prime attributes, while the rest are non-prime attributes.
     
  • The candidate key helps maintain the data's integrity and remove duplicity from the table.
     
  • In a database with many tables, we use the foreign key in one table to refer candidate key in another table to establish a relationship between the tables.
     
  • Assessing each row in a table is faster using the primary key, which is also a candidate key.

Types of Candidate Keys

In database design, there are different types of candidate keys that can be used to uniquely identify records within a table. These include:

Primary Key

A primary key is a candidate key chosen to identify each record in a table uniquely. It must be unique and not null, and there can only be one primary key per table. It enforces data integrity by preventing duplicates, which leads further to efficient data retrieval and indexing. It serves as the foundation for making a relationship between tables and is vital for maintaining the structure of the table.

Composite Key

A composite key consists of multiple columns that, when combined, form a unique identifier for a record. Each column may not be unique, but combining all the columns in the composite key must be unique. It is useful when no single column can uniquely identify records that allow for complex data relationships and integrity. These are common in many-to-many relationship tables and bridge tables in relational databases.

Unique Key

A unique key is a candidate key that ensures uniqueness, similar to a primary key. However, unlike a primary key, a table can have multiple unique keys. It allows for one NULL value. It also helps in maintaining data integrity providing efficient indexing, and it supports various relationships in a relational database.

Alternate Key

An alternate key is a candidate key that could have been chosen as the primary key but was not. It provides an alternative option for uniquely identifying records in a table. Alternate key offers additional options for querying and organizing data and can serve as a unique identifier in certain contexts. It plays a significant role in designing well-structured relational databases and establishing various relationships between tables.

Example of Candidate Key in DBMS

Consider a table Student containing information about students of a school:

Id

Roll Number

Name

Class

Date of Birth

Address

Email id

1 1 Shruti XII 24/1/2005 Delhi shruti@gmail.com
2 2 Ananya XII 1/3/2005 Noida ananay@gmail.com
3 3 Srijan VII 2/2/2009 Faridabad srijan@gmail.com
4 1 Minal X 2/9/2010 Delhi minal@gmail.com
5 3 Kartik XII 24/1/2005 Gurugram kartik@gmail.com

Here are some columns with which we can identify each row in the table uniquely:

  • Id
     
  • Email id
     
  • Id + roll number
     
  • roll number + class
     
  • Id + class
     
  • Id + date of birth
     
  • Email id+ date of birth
     

Can you find some other combinations too?

Here we have listed seven combinations. Are all of them candidate key? No, all of them are not candidate keys. Let's recall all the requirements for the candidate key.

  1. All the sets we have listed have unique values. So it fulfils the first condition.
     
  2. But see in the key Id + roll number; we can identify all the rows uniquely just by id. So the roll number is redundant here. Hence this is not a candidate key. Similarly, id+class, Id + date of birth, Email id+ date of birth. All have redundant attributes, and they are not candidate keys.
     
  3. If we see the key roll number + class. Each year the class and roll number will change for the student, so this is not consistent. Hence can not be used as a candidate key.


So we can say that candidate keys in this table are  Id, id+ roll number, email id.

If, among these, we choose ‘id’  as the primary key. Then the remaining candidate keys are also known as the alternate key.

Why do we Need a Candidate Key?

Candidate keys play a vital role in relational databases by ensuring data integrity and facilitating efficient data retrieval. A candidate key is a minimal set of attributes that can uniquely identify each tuple or row within a relation or table.

The need for candidate keys arises from the fundamental requirement of relational databases to maintain data integrity. Without a candidate key, it becomes challenging to ensure that each tuple in a table can be uniquely identified, leading to potential issues such as data duplication and inconsistency.

By designating one or more candidate keys for a table, database administrators can enforce entity integrity, ensuring that each tuple contains unique values for the designated attributes. This prevents data redundancy and anomalies like insertion, deletion, and update anomalies.

How is the Candidate Key Different from the Primary Key?

Candidate Key

Primary Key

Candidate Key can have null values. Primary key can not have null values.
There can be more than one Candidate Key in a table.  There can be only one Primary Key in each table.
Every Candidate Key is not a primary key Every Primary key is a Candidate key.
It is necessary to have a Candidate Key to define the relation between two tables. While expressing a relation between tables, having a primary key in that relationship is not necessary.
There is no default indexing on Candidate Key. There is c-based indexing for Primary Key by default.

Example:

Consider the employee table containing employee data

id

employee name

city

email id

1 Rajesh Delhi rajesh@xyzl.com
2 Sai  Hyderabad null
3 Rohan Mumbai rohan@xyz.com

 

In this table we â€˜id’‘email’ columns can be used to uniquely identify an employee. But it is possible that some employee has not been assigned to any email id yet. So email id can not be used as the primary key in this table, But it is still a candidate key.

Hence â€˜id’ is the Primary key in this table. ‘id’, and ‘email’ both are candidate key here.

Properties of Candidate Key

  • Uniqueness: Each candidate key must uniquely identify every tuple or row within the relation.
  • Minimality: No proper subset of a candidate key can uniquely identify tuples.
  • Irreducibility: Every attribute in a candidate key is necessary for uniqueness; removing any attribute would result in loss of uniqueness.
  • Non-redundancy: Candidate keys must not contain redundant attributes.
  • Stability: Candidate keys should ideally remain unchanged over time to ensure consistency and integrity of data.
  • Independence: Candidate keys should be independent of each other; one candidate key should not be derivable from another.

Frequently Asked Questions

What is primary key and candidate?

A primary key is a unique identifier for each record. It ensures data integrity. A candidate key is a set of attributes that can uniquely identify a record, but one can be a primary key.

What is a candidate key and super key?

A candidate key is a set of attributes that can uniquely identify each record in a table. It is a potential choice for the primary key. A super key is a set of attributes that can uniquely identify a record.

What is a candidate key example?

An example of a candidate key is in a database of students, where both "Roll no." and "AADHAR NUMBER" are candidate keys. They can uniquely identify each student and could potentially be chosen as the primary key.

What is a candidate key also known as?

A candidate key is also referred to as a superkey. Sometimes, it is also known as a secondary key or alternate key. The columns in it are called prime attributes.

Conclusion

In this blog, we learned about the Candidate key in DBMS, their properties, basic conditions, and the difference between Primary Keys and Candidate keys. I hope this article helped you understand the candidate key in a better way.

If you want to learn more DBMS topics, you can check out the following articles - 

 

Happy Learning!!

Live masterclass