Keys are an important part of SQL, and the primary, foreign, and candidate keys hold a significant value. The alternate key holds are crucial, yet we often overlook their importance. They are used to design our database, Ensure the integrity of our data, and create an efficient way to retrieve records. Intriguing right? Further in this article, we will discuss SQL Alternate Key in detail.
In simple terms, an Alternate key is a candidate key that is not a primary key. Everyone knows that the primary key uniquely identifies each record in the table. The alternate key also uniquely identifies the records in the table and holds the same purpose.
Creating an alternate key is making columns with unique constraints.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ...);
Let’s first create a table to learn about alternate keys.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
SSN VARCHAR(11) NOT NULL,
Email VARCHAR(255) NOT NULL,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);
Here, we can see that EmployeeID is our primary key. We also know two employees can’t have the same email address and SSN. Hence, we have to implement unique constraints for them.
ALTER TABLE Employees
ADD CONSTRAINT unique_ssn UNIQUE (SSN);
ALTER TABLE Employees
ADD CONSTRAINT unique_email UNIQUE (Email);
The above code will make Email and SSN unique. This makes them an alternate key to the table Students.
Also read: SQL: A Full Fledged Guide from Basics to Advanced Level
Let’s try inserting some sample data into our Employee table.
INSERT INTO Employees (EmployeeID, SSN, Email, FirstName, LastName)
VALUES (1, '123-45-6789', '[email protected]', 'John', 'Doe');
INSERT INTO Employees (EmployeeID, SSN, Email, FirstName, LastName)
VALUES (2, '987-65-4321', '[email protected]', 'Jane', 'Doe');
From the above two codes, we can see there is no problem, and the code is getting executed.
INSERT INTO Employees (EmployeeID, SSN, Email, FirstName, LastName)
VALUES (3, '123-45-6789', '[email protected]', 'John', 'Smith');
We can see that this fails as the SSN must be unique; let’s try a similar thing with email
INSERT INTO Employees (EmployeeID, SSN, Email, FirstName, LastName)
VALUES (4, '555-55-5555', '[email protected]', 'Johnny', 'Doe');
We can see that this fails as the email should be unique, [email protected] is already present in our database.
Also read: SQL For Data Science: A Beginner Guide!
Alternate key plays an important role in the SQL and database management systems. We should try implementing and using them to enhance integrity and flexibility. We can ensure a robust and flexible database with an understanding of alternate keys.
Ans. Any candidate key which is not a primary key is an alternate key.
Ans. There is less difference in the functionality of alternate and primary keys. A candidate key, which is not a primary key, is an alternate key. Both identify records uniquely.
Ans. A candidate key is a minimal attribute that uniquely identifies a record.
Ans. A super key is any combination of attributes uniquely identifying a record, including candidate keys.