CodeBlanks Logo Icon CodeBlanks

CHECK constraint

Add a column rule with CHECK.

UNIQUE

With UNIQUE you ensure all values in a column are distinct. Duplicate input is blocked.

CREATE TABLE customers (
  id INT PRIMARY KEY,
  email VARCHAR(100) UNIQUE
);

Email is unique; two people cannot share an email.

The table customers is created.

UNIQUE — details

UNIQUE allows NULL values (most databases allow multiple NULLs). PRIMARY KEY does not allow NULL. UNIQUE is ideal for secondary unique fields.

Practice UNIQUE

Create customers with id INT PRIMARY KEY and email VARCHAR(100) UNIQUE.

Starter code

query.sql
-- unique email
CREATE TABLE customers

Sample solution

query.sql
CREATE TABLE customers (
  id INT PRIMARY KEY,
  email VARCHAR(100) UNIQUE
)

Expected output

Tablo oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Unique

Question
CREATE TABLE customers (
  email VARCHAR(100) ____
);
Answer
CREATE TABLE customers (
  email VARCHAR(100) UNIQUE
);

Explanation: UNIQUE prevents the same email from being stored twice.

2 What is the difference between UNIQUE and PRIMARY KEY?

  • They are the same
  • UNIQUE does not create an index
  • UNIQUE allows NULL Correct answer
  • PRIMARY KEY allows NULL

Explanation: UNIQUE allows NULL; PRIMARY KEY does not.

3 CHECK constraint: match the terms

Term Meaning
UNIQUE unique
PRIMARY KEY primary
NOT NULL not null
CHECK check

All lessons in Beginner