![]() |
In MySQL, an alternate key is a column or set of columns that can uniquely identify a record, similar to a primary key, but isn’t chosen as the primary key. Alternate keys ensure data uniqueness and provide additional ways to access records. They play a vital role in maintaining data integrity and optimizing database queries. Understanding and implementing alternate keys effectively can enhance database reliability and efficiency, making them a crucial component in relational database design. Key Concepts
What is an Alternate Key?An Alternate key is one of the candidate keys that is not chosen as the Primary key of a relation. It refers to an attribute or a set of attributes that can enable the specification of a specific tuple in relation same to the primary key. But, while defining a relation, only one candidate key can be specified as the primary key, and the others are termed as the alternate key. To define an alternate key, you need to use the ‘UNIQUE‘ constraint, which ensures that all values in a column or a set of columns are distinct. Syntax:Here is the basic syntax for defining an alternate key:
For an existing table, you can add a unique constraint using:
Examples of MySQL Alternate KeyCreating a Table with an Alternate KeyLet’s create a table named ‘students‘ with an alternate key column. CREATE TABLE students ( In this table:
Inserting Data into the TableNow, let’s insert some data into the ‘students’ table and observe how the alternate key column behaves. INSERT INTO students (email, phone) VALUES ('[email protected]', '1234567890'); Viewing the Table DataTo verify the inserted data and see the alternate key values, execute: SELECT * FROM students;
Output: This query will retrieve all records from the students table. The expected output will look like this: ![]() Output Adding a New Unique Constraint to an Existing TableYou can add a new unique constraint to an existing table to define an alternate key. Let’s add a unique constraint to the ‘phone‘ column. ALTER TABLE students ADD CONSTRAINT unique_phone UNIQUE (phone);
Inserting Additional DataNow, let’s insert a new row into the students table to see the effect of the unique constraint on the ‘phone’ column: INSERT INTO students (email, phone) VALUES ('[email protected]', '2233445566');
Viewing the Table Data AgainSELECT * FROM students;
Output: ![]() Output By adding the unique constraint to the ‘phone’ column, we ensure that each phone number is unique in the table, effectively making it an alternate key. Attempting to Insert Duplicate ValuesLet’s try inserting a duplicate value in the ‘email‘ column to see how the unique constraint enforces uniqueness: INSERT INTO students (email, phone) VALUES ('[email protected]', '3344556677');
Output: ERROR 1062 (23000): Duplicate entry '[email protected]' for key 'students.email'
The insertion fails because the ‘email‘ column must have unique values, as enforced by the unique constraint. ConclusionThe concept of an alternate key in MySQL is very useful in maintaining the uniqueness of data in a table. The primary key is the first unique key for a table, but alternate keys also have unique constraints for the same table. By using alternate keys correctly, you ensure that your database remains reliable and consistent, providing additional qualifiers for identifying records uniquely. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |