Lesson 36 of 60 – NOT NULL
60%

MySQL NOT NULL Constraint

The NOT NULL constraint is used to ensure that a column must contain a value. A column defined with NOT NULL cannot store NULL.

Note: NOT NULL is useful when a value is required for every record, such as a student's name, course name, or admission date.

1. What is NOT NULL?

The NOT NULL constraint prevents a column from storing NULL values.

CREATE TABLE students (
    student_id INT,
    name VARCHAR(100) NOT NULL
);

Every student record must have a value in the name column.

2. Why Use NOT NULL?

NOT NULL is used when a value is required.

  • Student name
  • Course name
  • Admission date
  • Employee name
  • Product name
  • Required email address

3. NOT NULL Syntax

The basic syntax is:

column_name data_type NOT NULL

Example:

name VARCHAR(100) NOT NULL

4. Basic NOT NULL Example

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100)
);

Here, the name column cannot contain NULL.

5. Inserting a Valid Value

INSERT INTO students
(student_id, name, course)
VALUES
(1, 'Amit', 'Python');

This works because the name column contains a value.

6. Inserting NULL into NOT NULL Column

The following statement fails because name is NOT NULL:

INSERT INTO students
(student_id, name, course)
VALUES
(2, NULL, 'Java');

MySQL rejects the row because name cannot contain NULL.

7. NOT NULL and NULL

NULL means that a value is missing or unknown. It is not the same as:

  • 0
  • An empty string ''
  • The word 'NULL'
NULL
0
''
'NULL'

These represent different values or concepts in MySQL.

8. NOT NULL with Multiple Columns

You can apply NOT NULL to several columns.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL
);

All three columns require values.

9. NOT NULL and INSERT

When inserting a row, required NOT NULL columns should be supplied with valid values.

INSERT INTO students
(student_id, name, course, mobile)
VALUES
(1, 'Amit', 'Python', '9876543210');

This record satisfies the NOT NULL requirements.

10. Omitting a NOT NULL Column

If a NOT NULL column is omitted from an INSERT statement and has no suitable default value, MySQL rejects the insert in strict SQL modes.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

INSERT INTO students
(student_id)
VALUES
(1);

The name column has no value, so the insert fails in normal strict configurations.

11. NOT NULL with DEFAULT

NOT NULL can be combined with DEFAULT.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'Active'
);

If status is omitted during INSERT, MySQL uses the default value.

12. NOT NULL with PRIMARY KEY

A PRIMARY KEY is implicitly NOT NULL.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100)
);

The student_id column cannot contain NULL because it is the PRIMARY KEY.

13. NOT NULL with UNIQUE

UNIQUE and NOT NULL can be combined when a value must both exist and be unique.

CREATE TABLE users (
    user_id INT PRIMARY KEY,
    email VARCHAR(150) NOT NULL UNIQUE
);

Every user must provide an email, and duplicate emails are not allowed.

14. NOT NULL vs UNIQUE

NOT NULL UNIQUE
Prevents NULL values Prevents duplicate non-NULL values
Does not itself prevent duplicates Does not generally prevent NULL by itself
Can be used on many columns Can be used on many columns

15. NOT NULL vs PRIMARY KEY

NOT NULL PRIMARY KEY
Prevents NULL Prevents NULL and duplicate key values
Can be applied to many columns Only one PRIMARY KEY constraint per table
Does not uniquely identify a row Uniquely identifies a row

16. Adding NOT NULL to Existing Column

You can modify an existing column using ALTER TABLE.

ALTER TABLE students
MODIFY name VARCHAR(100) NOT NULL;

Before making the column NOT NULL, existing rows must not contain NULL values.

17. Removing NOT NULL

You can allow NULL values again by modifying the column without NOT NULL.

ALTER TABLE students
MODIFY name VARCHAR(100) NULL;

The name column can now store NULL values.

18. NOT NULL with VARCHAR

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    department VARCHAR(100)
);

The employee name must be provided, while department can be NULL.

19. NOT NULL with INT

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    age INT NOT NULL
);

The age column cannot contain NULL.

Note that 0 is a value and is different from NULL.

20. NOT NULL with DATE

CREATE TABLE admissions (
    admission_id INT PRIMARY KEY,
    student_name VARCHAR(100) NOT NULL,
    admission_date DATE NOT NULL
);

Both student_name and admission_date are required.

21. NOT NULL with DECIMAL

CREATE TABLE fees (
    fee_id INT PRIMARY KEY,
    student_id INT NOT NULL,
    amount DECIMAL(10,2) NOT NULL
);

Both student_id and amount must have values.

22. NOT NULL and Empty String

NOT NULL prevents NULL, but an empty string is still a string value.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

This is technically not NULL:

INSERT INTO students
(id, name)
VALUES
(1, '');

If an application should also reject empty strings, additional validation may be needed.

23. NOT NULL and Application Validation

Database constraints and application validation can work together.

For example, a registration form can check whether the name is entered before sending data to MySQL.

if(empty($name)){
    echo "Name is required.";
}

The database should still use NOT NULL when the value is required at the database level.

24. Checking NOT NULL Definition

Use DESCRIBE to inspect whether a column allows NULL.

DESCRIBE students;

The Null column shows whether NULL is allowed for each column.

25. SHOW CREATE TABLE

SHOW CREATE TABLE displays the complete table definition.

SHOW CREATE TABLE students;

You can use it to check NOT NULL, DEFAULT, PRIMARY KEY, UNIQUE, and other constraints.

26. Common NOT NULL Mistakes

  • Trying to insert NULL into a NOT NULL column
  • Forgetting to provide required values during INSERT
  • Adding NOT NULL when existing rows contain NULL
  • Confusing NULL with an empty string
  • Confusing NULL with zero
  • Assuming NOT NULL automatically prevents duplicate values
  • Forgetting to use a DEFAULT value when appropriate

27. Updating a NOT NULL Column

You cannot change a NOT NULL column to NULL unless the column allows NULL.

UPDATE students
SET name = NULL
WHERE student_id = 1;

If name is NOT NULL, MySQL rejects the update.

28. Practical Student Table

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    student_code VARCHAR(20) NOT NULL UNIQUE,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    admission_date DATE NOT NULL
);

Here, every student must have a student code, name, course, mobile number, and admission date.

29. NOT NULL with DEFAULT Example

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'Active',
    admission_date DATE NOT NULL
);

INSERT INTO students
(name, admission_date)
VALUES
('Amit', '2026-09-21');

The status column is NOT NULL, but because it has a DEFAULT value, MySQL can automatically use Active when status is omitted.

30. Complete NOT NULL Example

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    student_code VARCHAR(20) NOT NULL UNIQUE,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    fee DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    status VARCHAR(20) NOT NULL DEFAULT 'Active'
);

INSERT INTO students
(student_code, name, course, mobile)
VALUES
('STU001', 'Amit Kumar', 'Python', '9876543210');

INSERT INTO students
(student_code, name, course, mobile)
VALUES
('STU002', 'Priya Singh', 'Java', '9876543211');

SELECT *
FROM students;

DESCRIBE students;

SHOW CREATE TABLE students;

In this example, the important student information cannot be NULL, while fee and status receive default values when they are not supplied.

📌 Key Points

  • NOT NULL prevents a column from storing NULL values.
  • Use NOT NULL when a value is required.
  • NOT NULL does not prevent duplicate values.
  • NOT NULL is different from UNIQUE.
  • A PRIMARY KEY cannot contain NULL.
  • NOT NULL can be combined with UNIQUE.
  • NOT NULL can be combined with DEFAULT.
  • You can add or remove NOT NULL using ALTER TABLE.
  • Existing NULL values must be handled before making a column NOT NULL.
  • NULL is different from zero and an empty string.
  • DESCRIBE and SHOW CREATE TABLE can be used to inspect column definitions.

🧠 Quick Quiz

Question: What does the NOT NULL constraint do?