UNIQUE Constraint

SQL · Working with Tables

Theory

A UNIQUE constraint guarantees that no two rows share the same value in a column, or the same combination of values across several columns. email VARCHAR(150) UNIQUE stops two employees from registering the same email; UNIQUE (dept, emp_code) allows the same code in different departments but not twice in one.

The engine enforces it with a unique index, which also speeds up lookups on that column. Unlike a primary key, a table can have many UNIQUE constraints, and the columns may be nullable. Because NULL is unknown, most engines (PostgreSQL, MySQL, Oracle, SQLite) allow many NULLs in a UNIQUE column, while SQL Server allows only one. Uniqueness is checked exactly, so 'Asha@mail.in' and 'asha@mail.in' are different unless the collation ignores case.

Why we use it

Some business values must never repeat even though they are not the primary key: emails, phone numbers, PAN or GST numbers, product codes. UNIQUE enforces that rule in the database instead of hoping the application checks it.

When to use it

Use UNIQUE for alternate identifiers and natural keys when the primary key is a surrogate id. If the value must also always be present, combine it with NOT NULL. Use the primary key for the main row identifier.

How it works

  1. INSERT or UPDATE a row
  2. Is the value NULL? Yes: Usually allowed (SQL Server allows one NULL). No: Check the unique index.
  3. Does another row already have this value? Yes: Reject: duplicate value. No: Accept the row.
  4. Add the value to the unique index
  5. Values stay distinct

Syntax

column datatype UNIQUE

Example

CREATE TABLE employees (
  email VARCHAR(150) UNIQUE
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic