# Primary Keys & Uniqueness

> Use PRIMARY KEY, UNIQUE and NOT NULL so every row has a reliable identity and bad data is refused.

Subject: SQL | Lesson 4 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/primary-keys-and-uniqueness
Previous: [NULL & Three-Valued Logic](https://codewithanimation.in/sql/null-and-three-valued-logic.md) | Next: [Foreign Keys & Relationships](https://codewithanimation.in/sql/foreign-keys-and-relationships.md)

## Constraints are rules the database enforces

A constraint is a rule the database checks every time you insert or update a row. If a row breaks the rule, the database refuses it and tells you which rule failed.

`NOT NULL` means the column must have a value. `UNIQUE` means no two rows may share the same value in that column. `PRIMARY KEY` means the column is both unique and not null, so it identifies exactly one row.

A table can have at most one primary key, but any number of `UNIQUE` constraints. A key can also cover several columns together.

## Rows need an identity

SQL does not label rows, and two identical rows are allowed. Without a key you cannot say "this exact row", which makes updates, deletes and links between tables unreliable.

A primary key gives every row a stable identity. Other tables can then point at it, as you will see in the next lesson.

## Put the rules in the table

This `members` table needs a unique id, a name that is never missing, and an email that may be missing but never repeated:

```sql
CREATE TABLE members (
  id integer PRIMARY KEY,
  name text NOT NULL,
  email text UNIQUE
);
```

Inserting an `id` that already exists fails with `duplicate key value violates unique constraint`. Inserting a `NULL` name fails with `violates not-null constraint`. Both leave the table unchanged.

A `UNIQUE` column may hold `NULL`, and by default two `NULL` values do not count as equal. So you can have many rows with no email. Write `UNIQUE NULLS NOT DISTINCT` if you want only one.

## Common mistake: Thinking UNIQUE forbids NULL

`UNIQUE` only stops repeated values. It does not stop missing ones. If a value is required, add `NOT NULL` as well.

Another common mistake is using a real-world value such as an email as the primary key. People change emails. A plain `id` that never changes is usually safer.

## Tip: Ask what makes two rows the same thing

When you design a table, ask: what makes two rows the same thing? The column or columns that answer that question are the key.

## Try it: Try to break the rules

Press the buttons to insert rows into `members`. Watch which rule refuses each one, and notice that two rows with no email are both accepted.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/primary-keys-and-uniqueness

## Check yourself

### Question 1

Which two rules does `PRIMARY KEY` combine?

- A. UNIQUE and NOT NULL
- B. UNIQUE and CHECK
- C. NOT NULL and FOREIGN KEY

Correct answer: A

A primary key accepts the same data as a column that is both UNIQUE and NOT NULL.

- Why not B: A CHECK constraint is a separate rule you write yourself.
- Why not C: A foreign key links to another table. A primary key does not.

### Question 2

`email` is `UNIQUE` and allows NULL. You insert two members with no email. What happens?

- A. Both are accepted
- B. The second is refused because NULL equals NULL
- C. Both are refused because UNIQUE forbids NULL

Correct answer: A

By default two NULL values never collide in a UNIQUE column.

- Why not B: NULL is unknown, so two NULL values are not considered equal.
- Why not C: UNIQUE allows NULL. Add NOT NULL to forbid it.

### Question 3

How many primary keys can one table have?

- A. At most one, though it may cover several columns
- B. Any number
- C. Exactly one, because PostgreSQL requires it

Correct answer: A

A table has at most one primary key, but it can have many UNIQUE constraints.

- Why not B: That is true of UNIQUE constraints, not of primary keys.
- Why not C: PostgreSQL does not force a primary key, although it is usually best to have one.

## Remember this

- A constraint is a rule the database checks on every insert and update.
- PRIMARY KEY means unique and not null, and a table has at most one.
- UNIQUE stops repeated values but allows NULL, more than once by default.
- A refused row leaves the table unchanged, and the error names the rule that failed.
