---
title: "Difference between PRIMARY KEY vs UNIQUE KEY."  
description: "Difference between PRIMARY KEY vs UNIQUE KEY."  
author: "Anubhav Sharma"  
published: 2026-04-27  
updated: 2026-04-27  
canonical: https://www.mindstick.com/interview/34494/difference-between-primary-key-vs-unique-key  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 3 minutes  

---

# Difference between PRIMARY KEY vs UNIQUE KEY.

Here’s a clear, interview-ready difference between **PRIMARY KEY** and **UNIQUE KEY**:

### PRIMARY KEY

- Uniquely identifies each row in a table
- **Cannot contain NULL values**
- Only **one primary key per table**
- Automatically creates a **clustered index** (in most databases like SQL Server)
- Used as the **main identifier**

## Example:

```plaintext
CREATE TABLE Users (
    Id INT PRIMARY KEY,
    Name VARCHAR(100)
);
```

### UNIQUE KEY

- Ensures all values in a column are **unique**
- **Can contain NULL** (usually only one NULL allowed, depends on DB)
- Can have **multiple unique keys** in a table
- Creates a **non-clustered index** by default
- Used for **alternate uniqueness (like email, username)**

## Example:

```plaintext
CREATE TABLE Users (
    Id INT PRIMARY KEY,
    Email VARCHAR(100) UNIQUE
);
```

### Quick Comparison

| Feature | PRIMARY KEY | UNIQUE KEY |
| --- | --- | --- |
| Uniqueness | Yes | Yes |
| NULL Allowed | No | Yes (1 or more*) |
| Keys per Table | Only 1 | Multiple allowed |
| Purpose | Main identifier | Alternate unique |
| Index Type | Clustered (default) | Non-clustered |

* Depends on database (SQL Server allows one NULL per UNIQUE column)

### When to Use

- Use **PRIMARY KEY** → for main ID (like `UserId`)
- Use **UNIQUE KEY** → for fields like `Email`, `Phone`, `Username`

## Answers

### Answer by Anubhav Sharma

Here’s a clear, interview-ready difference between **PRIMARY KEY** and **UNIQUE KEY**:

### PRIMARY KEY

- Uniquely identifies each row in a table
- **Cannot contain NULL values**
- Only **one primary key per table**
- Automatically creates a **clustered index** (in most databases like SQL Server)
- Used as the **main identifier**

## Example:

```plaintext
CREATE TABLE Users (
    Id INT PRIMARY KEY,
    Name VARCHAR(100)
);
```

### UNIQUE KEY

- Ensures all values in a column are **unique**
- **Can contain NULL** (usually only one NULL allowed, depends on DB)
- Can have **multiple unique keys** in a table
- Creates a **non-clustered index** by default
- Used for **alternate uniqueness (like email, username)**

## Example:

```plaintext
CREATE TABLE Users (
    Id INT PRIMARY KEY,
    Email VARCHAR(100) UNIQUE
);
```

### Quick Comparison

| Feature | PRIMARY KEY | UNIQUE KEY |
| --- | --- | --- |
| Uniqueness | Yes | Yes |
| NULL Allowed | No | Yes (1 or more*) |
| Keys per Table | Only 1 | Multiple allowed |
| Purpose | Main identifier | Alternate unique |
| Index Type | Clustered (default) | Non-clustered |

* Depends on database (SQL Server allows one NULL per UNIQUE column)

### When to Use

- Use **PRIMARY KEY** → for main ID (like `UserId`)
- Use **UNIQUE KEY** → for fields like `Email`, `Phone`, `Username`


---

Original Source: https://www.mindstick.com/interview/34494/difference-between-primary-key-vs-unique-key

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
