A Unique Key in SQL is a constraint used to ensure that all values in a column are unique.
In simple words:
A unique key prevents duplicate values in a column.
Why Unique Key is Important
In databases, some fields must contain unique values.
Examples:
- Email Address
- Phone Number
- Username
- Employee ID
Duplicate values in these columns can create serious issues.
Unique keys solve this problem.
Real-Time Example
Suppose a learning platform stores student details.
Students Table
| ID | Name | |
|---|---|---|
| 1 | Naresh | naresh@gmail.com |
| 2 | Rahul | rahul@gmail.com |
Understanding Unique Key
Here:
should be unique.
Reason:
- Two students cannot use same email address
Unique Key Example
email VARCHAR(100) UNIQUE
Unique Key Architecture
Students Table ---------------------------------------------------- | ID | Name | Email | ---------------------------------------------------- | 1 | Naresh | naresh@gmail.com | | 2 | Rahul | rahul@gmail.com | ----------------------------------------------------
How to Create Unique Key
Unique key is created using:
UNIQUE
SQL Syntax
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);
Explanation
| Column | Purpose |
|---|---|
| id | Primary Key |
| name | Student Name |
| Unique Key |
What Happens if Duplicate Value is Inserted?
Database throws an error.
Example
INSERT INTO students VALUES (1, 'Naresh', 'naresh@gmail.com'); INSERT INTO students VALUES (2, 'Rahul', 'naresh@gmail.com');
Error
Duplicate entry 'naresh@gmail.com' for key 'email'
Why Unique Key is Useful
Unique keys ensure:
- Data uniqueness
- Data integrity
- Reliable user identification
Related Learning Topics
- What is a Database?
- What is a Table in SQL?
- What is a Primary Key in SQL?
- What is a Foreign Key in SQL?
Unique Key vs Primary Key
| Feature | Primary Key | Unique Key |
|---|---|---|
| Purpose | Uniquely identifies row | Prevents duplicate values |
| NULL Allowed | No | Yes (usually one NULL) |
| Count Per Table | Only One | Multiple Allowed |
| Automatically Indexed | Yes | Yes |
Important Difference
A table can have:
- Only one primary key
- Multiple unique keys
Example
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
phone VARCHAR(15) UNIQUE
);
Explanation
Here:
- id = primary key
- email = unique key
- phone = unique key
Can Unique Key Contain NULL?
Yes.
Most databases allow:
One NULL value
in unique key columns.
Example
INSERT INTO students VALUES (1, 'Naresh', NULL);
Why NULL is Allowed
NULL means:
Unknown Value
Unknown values are not considered duplicates.
Composite Unique Key
A unique key can also use multiple columns together.
Example
CREATE TABLE enrollments (
student_id INT,
course_id INT,
UNIQUE(student_id, course_id)
);
Why Composite Unique Key is Used
Prevents:
- Same student enrolling in same course multiple times
Unique Key Query Flow
Application
|
v
INSERT Query
|
v
Unique Key Validation
|
v
Duplicate Check
|
v
Data Stored
How Unique Keys Improve Performance
Unique keys automatically create indexes.
Indexes improve:
- Search performance
- Query speed
- Data retrieval
Example Query
SELECT * FROM students WHERE email = 'naresh@gmail.com';
Real-Time Banking Example
Banking systems use unique keys for:
- Account Number
- Customer Email
- PAN Number
Real-Time E-Commerce Example
E-commerce systems use unique keys for:
- Username
- Email Address
- Order Number
Real-Time Learning Platform Example
Learning platforms use unique keys for:
- Student Email
- Certificate ID
- Username
Advantages of Unique Key
- Prevents duplicate values
- Maintains data integrity
- Improves searching
- Supports indexing
Challenges of Unique Key
- Duplicate cleanup during migration
- Index maintenance overhead
- Additional validation cost
Best Practices
- Use unique keys for business-critical fields
- Use email and username as unique
- Avoid unnecessary unique constraints
- Use composite unique keys carefully
Unique Key in Microservices
In microservices architecture:
- Each service database may define unique constraints independently
Example
Auth Service
|
v
email UNIQUE
Payment Service
|
v
transaction_id UNIQUE
Real Production Example
In an e-commerce platform:
- Order ID must be unique
- Customer email must be unique
- Payment transaction ID must be unique
This prevents:
- Duplicate orders
- Duplicate accounts
- Payment conflicts
Unique Key vs Foreign Key
| Feature | Unique Key | Foreign Key |
|---|---|---|
| Purpose | Prevents duplicates | Creates relationships |
| Uniqueness Required | Yes | No |
| References Another Table | No | Yes |
Professional Interview Answer
A Unique Key in SQL is a constraint used to ensure that all values in a column or combination of columns remain unique. It prevents duplicate values and helps maintain data integrity. Unlike a primary key, a table can have multiple unique keys, and unique key columns may allow NULL values depending on the database system. Unique keys are commonly used for fields such as email addresses, usernames, phone numbers, transaction IDs, and order numbers.
Why Interviewers Like This Answer
- Clearly explains uniqueness concept
- Includes SQL examples
- Compares with primary key
- Shows database design understanding
- Includes real-world applications
Frequently Asked Questions
What is a unique key in SQL?
A unique key prevents duplicate values in a column.
Can unique key contain NULL?
Yes, most databases allow one NULL value.
Can a table have multiple unique keys?
Yes, multiple unique keys are allowed.
What is difference between primary key and unique key?
Primary key uniquely identifies rows and cannot contain NULL, while unique key prevents duplicates and may allow NULL values.
Why unique key is important?
Unique keys maintain data integrity and prevent duplicate business records.