MySQL + SQL · Lesson 39
NOT NULL, UNIQUE, CHECK and DEFAULT
Column-Level Constraints
These constraints control what values a column will accept, keeping data clean.
Each Constraint
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50) NOT NULL, -- must have a value
email VARCHAR(60) UNIQUE, -- no duplicates
marks INT CHECK (marks BETWEEN 0 AND 100), -- valid range
status VARCHAR(10) DEFAULT 'active' -- auto value
);
Quick Meaning
| Constraint | Rule |
|---|---|
| NOT NULL | cannot be empty |
| UNIQUE | no repeated values |
| CHECK | value must pass a condition |
| DEFAULT | used when no value is given |
Summary
- NOT NULL = required; UNIQUE = no duplicates.
- CHECK = condition must hold; DEFAULT = auto value.