Difference between Primary Key and Candidate Key

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

In a Database Management System (DBMS), keys serve as labels for information, enabling efficient data retrieval. Key types in a DBMS include the Primary Key, Foreign Key, Candidate Key, Super Key, and more. The Primary Key uniquely identifies each record in a table, ensuring no NULL values. Conversely, a Candidate Key, while also unique, allows NULL values. This article concentrates on two key types: Primary and Candidate Keys, and talks about the difference between primary key and candidate key.

What is a Primary Key?

A primary key allows us to identify a specific record in a relational database with certainty. It's a special, individual marker for every piece of information or row of data. This marker guarantees that no two pieces of information share the same tag.

For example, let's say we have a table of students. Each student has a roll number, an Aadhar number, a first name, a middle name, a last name, and an age. The primary key here could be either the student's roll number or their Aadhar ID. The names and ages might be the same for different students, but the roll number or Aadhar ID will always be unique for each student.

Therefore, a primary key functions as a unique identifier, assisting us in distinguishing between various elements and maintaining clarity and organization within our data environment.

It's important to note that a table can have only one primary key.

What is a Candidate Key?

In a relational database, a candidate key refers to either a single column or a combination of multiple columns that could potentially serve as a primary key. The term "candidate key" is used to describe a "minimal super key," with the distinction that a candidate key is allowed to possess a NULL value, unlike a primary key. Whether a candidate key becomes a primary key or not depends on the circumstances.

Within a table, there can be multiple candidate keys, but only one among them can be designated as the primary key. The remaining candidate keys are referred to as "alternate keys." Typically, the selection of a candidate key as the primary key is based on its appropriateness and relevance to the data stored in the table.

For instance, consider the same example of a student table having a roll number, an Aadhar number, a first name, a middle name, a last name, and an age. In this situation, both "ID" and Aadhar_ID can be recognized as candidate keys. As a result, this table possesses multiple candidate keys, each with the ability to uniquely distinguish a tuple within the relation.

Difference between Primary Key and Candidate Key

Now, let's understand the difference between the primary key and the candidate key based on different aspects and parameters:

AspectPrimary KeyCandidate Key
Null Values AllowedThe value in the primary key column can never be NULL.The column of the candidate key is allowed to contain a NULL value.
Key in Primary RoleThe primary key also serves as a candidate key.A candidate key may or may not fulfill the role of a primary key.
Number per TableEach table can feature only a single primary key.A table can possess multiple candidate keys.
Key in Relationship RoleWe don't always have to choose a primary key for every relationship.When creating a relationship, it's important to define the candidate key.
Minimal Super KeyThe primary key stands as a minimal super key.Unlike the primary key, the candidate key doesn't inherently function as a minimal super key.
Selection ProcessThe primary key is selected from a pool of candidate keys for the table.The candidate key is chosen from the larger set of super keys for the table.
ExampleThe ISBN number of a book, which is unique for each book, is chosen as the primary key in the library database.The combination of a book's title and author, which can also uniquely identify books, is considered a candidate key.

Conclusion

  • There are various types of keys in a DBMS, including Primary Key, Foreign Key, Candidate Key, Super Key, and Composite Key.
  • The primary key is a special key that uniquely identifies each record in a table.
  • Candidate keys are potential primary keys and can be single columns or combinations of columns
  • The primary key is always unique and non-NULL, serving as a minimal super key.
  • A candidate key may allow NULL values and might or might not be chosen as the primary key.
  • While a primary key is essential to identify individual records, a candidate key plays a crucial role in establishing relationships between tables, and ensuring data consistency.