What is Candidate Key and Alternate Key?

In the context of relational databases, candidate keys and alternate keys are related concepts.

Candidate Key:

  • A candidate key is a set of one or more columns (attributes) that can uniquely identify a tuple (row) in a relation (table).
  • It must satisfy two properties: uniqueness (no two distinct tuples can have the same values for the candidate key) and irreducibility (removing any attribute from the key would result in the loss of uniqueness).
  • In a table, there can be multiple candidate keys, but one of them is chosen to be the primary key.

Primary Key:

  • The primary key is a candidate key that is chosen to be the main method of identifying rows in a table.
  • It is used to establish relationships between tables in a relational database.
  • By definition, a primary key must be unique and not contain NULL values.

Alternate Key:

  • An alternate key is any candidate key that is not selected as the primary key.
  • While the primary key uniquely identifies each record in the table, alternate keys provide an alternative means of identification.
  • In some cases, an alternate key may be used as a unique identifier in other contexts or for specific purposes.

In summary, candidate keys are sets of columns that could potentially be used as unique identifiers for rows in a table. The primary key is the chosen candidate key that uniquely identifies each row in the table, and alternate keys are the remaining candidate keys that are not selected as the primary key.