CHECK constraint
- SQL course
- Beginner
- Lesson 96/115
- 4 min
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
-- unique email
CREATE TABLE customersSample solution
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
CREATE TABLE customers (
email VARCHAR(100) ____
);
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 |