đ The UNIQUE constraint in SQL is used to ensure that all values in a column or a combination of columns are unique. It prevents duplicate values from being inserted into the table, helping maintain data integrity and consistency.
đ What is the UNIQUE Constraint?
The UNIQUE constraint guarantees that no two rows contain the same value (or combination of values) in the constrained column(s). Unlike a PRIMARY KEY, a table can have multiple UNIQUE constraints.
Information
đ¯ Why Use UNIQUE?
The UNIQUE constraint is useful whenever duplicate values are not allowed.
- đ Prevent duplicate email addresses.
- đ Ensure usernames are unique.
- đ Avoid duplicate employee IDs or product codes.
- đ Maintain data accuracy.
- đ Improve data integrity.
đ Basic Syntax
UNIQUE Constraint Syntax
CREATE TABLE table_name
(
column_name data_type UNIQUE
);đĄ Create a Table with UNIQUE
Create a Students table where every student email address must be unique.
UNIQUE Column Example
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(150) UNIQUE,
Department VARCHAR(100)
);In this example, no two students can have the same email address.
â Valid INSERT
This statement succeeds because the email address is unique.
Valid INSERT
INSERT INTO Students
(StudentID, Name, Email, Department)
VALUES
(101, 'Alice', 'alice@example.com', 'Computer Science');â Invalid INSERT
Attempting to insert another row with the same email address causes an error.
Duplicate UNIQUE Value
INSERT INTO Students
(StudentID, Name, Email, Department)
VALUES
(102, 'Bob', 'alice@example.com', 'Mathematics');Error
đ Composite UNIQUE Constraint
A UNIQUE constraint can also apply to multiple columns. In this case, the combination of values must be unique.
Composite UNIQUE Constraint
CREATE TABLE Enrollments
(
StudentID INT,
CourseID INT,
Semester VARCHAR(20),
CONSTRAINT UQ_Enrollment
UNIQUE (StudentID, CourseID)
);A student cannot enroll in the same course more than once, but the same course can be taken by different students.
â Add a UNIQUE Constraint to an Existing Table
You can add a UNIQUE constraint after creating a table.
Add UNIQUE Constraint
ALTER TABLE Students
ADD CONSTRAINT UQ_Students_Email
UNIQUE (Email);â Remove a UNIQUE Constraint
Removing a UNIQUE constraint requires database-specific syntax.
Drop UNIQUE Constraint (SQL Server Example)
ALTER TABLE Students
DROP CONSTRAINT UQ_Students_Email;Important
đ Example Table
| StudentID | Name | |
|---|---|---|
| 101 | Alice | alice@example.com |
| 102 | Bob | bob@example.com |
âī¸ PRIMARY KEY vs UNIQUE
| Feature | PRIMARY KEY | UNIQUE |
|---|---|---|
| Duplicate Values Allowed | â No | â No |
| NULL Values | â Not allowed | Depends on the database system. |
| Number Allowed Per Table | One | Multiple |
| Typical Purpose | Identify each row. | Prevent duplicate values. |
đŧ Real-World Example
In an online learning platform, every user must register with a unique email address to ensure each account can be identified correctly.
Users Table
CREATE TABLE Users
(
UserID INT PRIMARY KEY,
Username VARCHAR(50) UNIQUE,
Email VARCHAR(150) UNIQUE,
PasswordHash VARCHAR(255)
);This design ensures that both usernames and email addresses remain unique across all users.
â ī¸ Common Mistakes
- â Adding a UNIQUE constraint when duplicate data already exists.
- â Assuming PRIMARY KEY and UNIQUE are identical.
- â Forgetting that composite uniqueness applies to the combination of columns.
- â Not understanding how the chosen database handles NULL values.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ UNIQUE prevents duplicate values.
- đ Multiple UNIQUE constraints can exist in a table.
- đ Composite UNIQUE constraints enforce uniqueness across multiple columns.
- đ PRIMARY KEY is always unique, but not every UNIQUE column is a primary key.
- đ Database systems may handle NULL values differently.
- đ UNIQUE helps maintain accurate and reliable data.