Beginner

135. UNIQUE KEY

Understand how a unique key ensures that duplicate values are not stored.

📘 Lesson 135 of 728 ⏱ 8 min 🎯 Beginner

A unique key or UNIQUE constraint ensures that values in a specified column or group of columns remain unique according to the database rules.

Many business attributes must be unique even though they are not the primary identifier of a table.

An employee's official email address may need to be unique even though EmployeeID is the primary key.

SQL
CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    Email VARCHAR(150) UNIQUE
);

Unique keys are useful for email addresses, usernames, account numbers, employee codes, and business reference numbers.

  • UNIQUE prevents duplicate values.
  • A table can have multiple unique constraints.
  • A UNIQUE constraint can involve multiple columns.
  • It is different from a primary key.
  • NULL handling depends on the database system.
What is a unique key?

A unique key ensures that duplicate values are not allowed for the constrained column or column combination.

Can a table have multiple unique keys?

Yes. A table can generally have multiple UNIQUE constraints.

Have you finished this lesson?

Introduction to SQL – All Lessons