By default, any column in a MySQL table can hold a NULL value — meaning a row can be saved without providing a value for that column. The NOT NULL constraint changes this behavior: it forces a column to always contain a value. If you try to insert or update a row without providing a value for a NOT NULL column, MySQL will reject it with an error.
This is one of the simplest yet most important constraints in MySQL, because it protects your data from being incomplete in places where a value is absolutely required.
Understanding NULL in MySQL is important before working with the constraint. NULL does not mean zero or an empty string — it means no value at all, or unknown.
The column accepts NULL. A row can be saved even if no value is provided for this column. The field is simply left empty.
The column rejects NULL. Every row must supply a real value for this column, or MySQL will throw an error.
NULL is not the same as an empty string ('') or the number 0. It is the complete absence of a value. A column containing an empty string still has a value — just an empty one. A column containing NULL has nothing at all.
You apply NOT NULL directly after the column's data type when creating or modifying a table:
column_name data_type NOT NULL
The most common place to define NOT NULL is when you first create a table. Any column where a value is always required should be marked with NOT NULL.
The example below creates a students table where both name and email are required fields:
CREATE TABLE students (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL,
phone VARCHAR(20),
PRIMARY KEY (id)
);
In this table:
id, name, and email are required — they cannot be left empty.phone has no constraint, so it is optional — a student can be added without a phone number.If you try to insert a row and leave out a NOT NULL column, MySQL immediately rejects the operation and returns an error:
-- This will fail because 'name' is NOT NULL
INSERT INTO students (email) VALUES ('sara@example.com');
-- ERROR 1364 (HY000): Field 'name' doesn't have a default value
To insert successfully, every NOT NULL column must receive a value:
-- Correct — all NOT NULL columns are provided
INSERT INTO students (name, email) VALUES ('Sara Ahmed', 'sara@example.com');
NOT NULL and you forgot to include it in your INSERT statement.
If a table already exists and you want to enforce NOT NULL on a column, use ALTER TABLE with MODIFY COLUMN:
ALTER TABLE students
MODIFY COLUMN phone VARCHAR(20) NOT NULL;
NOT NULL to an existing column, make sure that no rows in the table already contain NULL for that column. If any rows have NULL, MySQL will refuse the change and return an error. You must first fill in those missing values.
You can find rows with missing values before making the change:
-- Check for rows where phone is NULL
SELECT * FROM students WHERE phone IS NULL;
Then update those rows with a value before altering the column:
UPDATE students SET phone = 'N/A' WHERE phone IS NULL;
Now the ALTER TABLE command will succeed.
To make a column optional again — allowing NULL values — use ALTER TABLE and simply omit NOT NULL from the column definition:
ALTER TABLE students
MODIFY COLUMN phone VARCHAR(20);
After this change, the phone column will accept NULL values again, making it optional when inserting rows.
You can inspect which columns have NOT NULL enforced by using the DESCRIBE command:
DESCRIBE students;
MySQL returns a result like this:
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | NULL | ||
| varchar(150) | NO | NULL | |||
| phone | varchar(20) | YES | NULL |
The Null column shows NO for columns with the NOT NULL constraint and YES for columns that allow NULL.
NULL. NOT NULL overrides this behavior.NOT NULL is declared directly after the column data type in a CREATE TABLE or ALTER TABLE statement.NOT NULL column will cause MySQL to return an error.NOT NULL to an existing column, ensure no current rows have NULL in that column.DESCRIBE table_name to check which columns enforce NOT NULL — look for NO in the Null column.NULL is not the same as 0 or an empty string — it means the value is completely absent.NOT NULL, the next lesson covers the UNIQUE constraint — which ensures that every value in a column is different across all rows.