SQL Alternate Key

An Alternate Key in SQL is a candidate key that is not selected as the primary key. In other words, it is any key that could have been chosen as the primary key but wasn’t. An alternate key can still be used to uniquely identify a record in a table, ensuring that no two rows in the table have the same values for that key.

Typically, a table can have multiple candidate keys, and one of them is selected as the primary key. The remaining candidate keys are known as alternate keys. Each alternate key is unique, just like the primary key, but it is not the one chosen to represent the table's identity.

Why are Alternate Keys Important?
Alternate keys are useful because they provide a mechanism for ensuring data integrity across multiple attributes, not just the one chosen as the primary key. For example, if we want to ensure that both an employee's `EmployeeID` and `Email` are unique, we would define both of these columns as candidate keys, where `EmployeeID` becomes the primary key and `Email` becomes the alternate key.

Difference Between Primary Key and Alternate Key:

Primary Key: The primary key is a column or set of columns that uniquely identifies a record in the table. Each table can have only one primary key.
Alternate Key: An alternate key is any candidate key that is not selected as the primary key. A table can have multiple alternate keys.

Tables Used

Employees Table:
EmployeeID EmployeeName Email
101 Alice alice@example.com
102 Bob bob@example.com
103 Charlie charlie@example.com

Example with Alternate Key

In this example, we will create an Employees table where both the EmployeeID and Email columns are candidate keys. We will select EmployeeID as the primary key and use Email as an alternate key.

Code Example


-- Create the Employees table with EmployeeID as the primary key
-- and Email as the alternate key
CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(100),
    Email VARCHAR(100) UNIQUE
);

-- Insert data into the Employees table
INSERT INTO Employees (EmployeeID, EmployeeName, Email) VALUES (101, 'Alice', 'alice@example.com');
INSERT INTO Employees (EmployeeID, EmployeeName, Email) VALUES (102, 'Bob', 'bob@example.com');
INSERT INTO Employees (EmployeeID, EmployeeName, Email) VALUES (103, 'Charlie', 'charlie@example.com');

-- View the Employees table
SELECT * FROM Employees;
        

Output Table

EmployeeID EmployeeName Email
101 Alice alice@example.com
102 Bob bob@example.com
103 Charlie charlie@example.com

Explanation

- In this example, EmployeeID is chosen as the Primary Key for the Employees table, ensuring each employee has a unique ID.
- The Email column is defined as Unique, making it an Alternate Key. It ensures that no two employees can have the same email address, even though it is not the primary key.
- Both EmployeeID and Email could uniquely identify a record, but EmployeeID was chosen as the primary key while Email remains as an alternate key.

Additional Theory Behind Alternate Keys

Uniqueness: An alternate key must maintain the uniqueness property. This ensures that no two records in the table can have the same values for the columns that make up the alternate key.
Data Integrity: The purpose of both primary and alternate keys is to enforce data integrity. While the primary key is used to ensure a unique identifier for each record in the table, alternate keys provide additional means of identifying records.
Flexibility: Alternate keys provide flexibility in cases where the primary key does not make sense for certain queries. For instance, in the example above, if you wanted to uniquely identify an employee by their email rather than by their employee ID, the `Email` column can be used as a unique constraint.

In summary, the alternate key offers an additional method of enforcing uniqueness, and can be used for additional constraints, indexes, or queries that require an alternative means of identifying records. It is important to define alternate keys correctly to maintain the integrity and structure of a relational database.