What is the Difference Between Primary Key and Candidate Key?

🆚 Go to Comparative Table 🆚

The primary key and candidate key are both used to uniquely identify records in a table, but they have some differences:

  1. Number of keys: A table can have only one primary key, while it can have one or more candidate keys.
  2. NULL values: Candidate keys can contain NULL values, unlike primary keys, which must be non-null and unique.
  3. Minimal super key: A primary key is a minimal super key, meaning it is a key that cannot be reduced any further without losing the ability to uniquely identify a record.

In summary, both primary keys and candidate keys are used to uniquely identify records in a table, but a primary key is a non-null, unique key that can be a minimal super key, while a candidate key can have NULL values and there can be multiple candidate keys in a table.

Comparative Table: Primary Key vs Candidate Key

Here is a table comparing the differences between primary keys and candidate keys:

Feature Primary Key Candidate Key
Uniqueness Identifies each record uniquely Identifies each record uniquely
Minimal Super Key Yes, it is a minimal super key Yes, it can be a super key but not necessarily a minimal super key
Null Values Cannot contain NULL values Can contain NULL values
Number of Keys Each table can have only one primary key Each table can have multiple candidate keys
Selection Chosen among candidate keys to maintain uniqueness in a table Selected from the set of super keys, ensuring it does not have any redundant attribute
Examples In a table 'Employee': EmployeeID, SSN are candidate keys. You can choose either EmployeeID or SSN as the primary key. In a table 'Employee': EmployeeID & SSN are candidate keys. Any one of them can be chosen as the primary key.

In summary, primary keys are unique and non-null attributes that identify each record in a table, and only one primary key can exist in a table. On the other hand, candidate keys are also unique attributes that identify each record in a table, but multiple candidate keys can exist in a table, and one of them is chosen as the primary key.