Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 40

NOT NULL, UNIQUE, CHECK and DEFAULT in MySQL

In this lesson: Use NOT NULL, UNIQUE, CHECK and DEFAULT constraints in MySQL with CREATE/ALTER examples and error output.

What are constraints

Constraints are rules you put on columns to keep data valid automatically. Instead of checking values in your program every time, the database itself enforces them. The most common are NOT NULL, UNIQUE, DEFAULT and CHECK.

💡 Constraints protect your data: no empty required fields, no duplicates, sensible defaults, valid values.

NOT NULL

CREATE TABLE students (
    roll INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
);

NOT NULL means the column must always have a value. Trying to insert a student without a name fails. Use it for required fields that must never be empty.

UNIQUE

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

UNIQUE stops duplicate values in a column. Two users cannot share the same email. Unlike a primary key, UNIQUE can allow one null and a table can have several UNIQUE columns.

DEFAULT

CREATE TABLE orders (
    id INT PRIMARY KEY,
    status VARCHAR(10) DEFAULT 'pending'
);
INSERT INTO orders (id) VALUES (1);   -- status becomes 'pending'

DEFAULT gives a fallback value used when you do not supply one. Here a new order with no status automatically becomes 'pending', saving you from setting it every time.

CHECK

CREATE TABLE students (
    roll INT PRIMARY KEY,
    marks INT CHECK (marks >= 0 AND marks <= 100)
);

CHECK enforces a condition on the value. Here marks must be between 0 and 100; MySQL rejects any row that breaks this. It guarantees values stay sensible.

All together

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE,
    age INT CHECK (age >= 18),
    active BOOLEAN DEFAULT TRUE
);

You can combine constraints on one table. Here every employee must have a name, a unique email, an age of at least 18, and is active by default. The database enforces all these rules for you.

Common mistakes

  • Missing NOT NULL on required fields. Then empty values slip in.
  • Confusing UNIQUE and PRIMARY KEY. UNIQUE allows one null; primary key does not.
  • Forgetting DEFAULT applies only when the column is omitted. Passing NULL explicitly overrides it.
  • Assuming CHECK works on very old MySQL. CHECK is enforced from MySQL 8.0.16 onward.

Summary

  • Constraints are automatic rules that keep data valid.
  • NOT NULL requires a value; UNIQUE prevents duplicates.
  • DEFAULT supplies a fallback value when none is given.
  • CHECK enforces a condition, like a valid range.
  • You can combine several constraints on one table.

Next, learn the key constraints in primary key vs foreign key, or change columns with ALTER TABLE.

Frequently Asked Questions

What are constraints in MySQL?
Constraints are rules on columns that keep data valid. Common ones are NOT NULL (no empty value), UNIQUE (no duplicates), DEFAULT (a fallback value), and CHECK (a condition the value must meet). They protect data quality automatically.
What does NOT NULL do?
NOT NULL means a column must always have a value — you cannot insert or leave it empty (null). It is used for required fields like a name or an id that must never be missing.
What is the difference between UNIQUE and PRIMARY KEY?
Both prevent duplicate values. A PRIMARY KEY also cannot be null and there is only one per table. A UNIQUE constraint can allow one null and a table can have several UNIQUE columns.
What does the DEFAULT constraint do?
DEFAULT gives a column a fallback value used when you do not provide one during INSERT. For example status VARCHAR(10) DEFAULT 'active' makes new rows active unless you say otherwise.
What is a CHECK constraint?
A CHECK constraint enforces a condition on the value, like CHECK (age >= 18). MySQL rejects any row that breaks the condition, so you can guarantee, for example, that marks are never negative.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicAUTO_INCREMENT in MySQL Your progress: 40 / 151 (26%) · Lesson 41 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free