What is a Unique Key in SQL?
A Unique Key in SQL is a constraint that ensures all values in a column (or combination of columns) are distinct across rows within a table. This means no two rows can have the same value in the unique key column(s). It helps maintain data integrity by preventing duplicate entries.
Key Characteristics of a Unique Key
Uniqueness:
Each value in a unique key column must be distinct.Allows Null Values:
Unlike primary keys, unique keys allow one or moreNULL
values, depending on the database system. However, theseNULL
values cannot violate the uniqueness rule (e.g., two rows cannot have the same non-null value in a unique key).Multiple Unique Keys:
A table can have multiple unique keys, each ensuring the uniqueness of its respective columns.Index Creation:
Unique keys automatically create a unique index in the database, which optimizes searches.
Syntax for Creating a Unique Key
While Creating a Table:
CREATE TABLE users (
user_id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE,
username VARCHAR(255) UNIQUE
);
- Here,
email
andusername
columns are unique keys, ensuring no two users have the same email or username.
Adding a Unique Key to an Existing Table:
ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);
Examples
Inserting Data into a Table with Unique Keys:
INSERT INTO users (user_id, email, username)
VALUES (1, 'user@example.com', 'user123');
- If you try to insert a duplicate value in the
email
orusername
columns:
INSERT INTO users (user_id, email, username)
VALUES (2, 'user@example.com', 'user456');
- This query will fail because the
email
value already exists.
Difference Between Primary Key and Unique Key
Aspect | Primary Key | Unique Key |
---|---|---|
Uniqueness | Ensures unique values. | Ensures unique values. |
Null Values | Does not allow NULL values. |
Allows NULL values (varies by system). |
Number in Table | Only one primary key per table. | Multiple unique keys per table. |
Why Use a Unique Key?
Prevent Duplicate Data:
Ensures important fields, like emails or usernames, remain unique.Support Business Rules:
Enforces data integrity by maintaining constraints specific to the application.Optimize Queries:
The underlying unique index helps speed up data retrieval.
Practical Use Cases
User Authentication:
Ensure unique emails or usernames for account creation.Inventory Management:
Prevent duplicate product IDs or barcodes.Banking Systems:
Enforce unique account numbers or card details.
Conclusion
A unique key is a vital SQL feature for maintaining data integrity and preventing duplicate entries in your database. It complements primary keys by allowing for multiple unique constraints in a table, making it a versatile tool in database design.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
Top comments (0)