SQL Tutorial › Database Design › NOT NULL

SQL NOT NULL Constraint

BeginnerLesson 51 of 697 min read6 queries run on MySQL 8.4

By default any column may hold NULL. A NOT NULL constraint says “this column must always have a value”. It is the simplest and most useful constraint: names, dates and amounts that are required by the business should never be missing, and the database can guarantee that.

Advertisement
In this lesson
  • Declare required columns with NOT NULL
  • Recognise NOT NULL errors on INSERT and UPDATE
  • Add NOT NULL to an existing column safely
  • Combine NOT NULL with DEFAULT

Define NOT NULL columns

MySQL
CREATE TABLE patients (  patient_id INT PRIMARY KEY,  full_name  VARCHAR(60) NOT NULL,  phone      VARCHAR(15),  admitted   DATE NOT NULL);DESCRIBE patients;
ResultMySQL 8.4
mysql> CREATE TABLE patients (
Query OK, 0 rows affected
mysql> DESCRIBE patients
FieldTypeNullKeyDefaultExtra
patient_idintNOPRINULL
full_namevarchar(60)NONULL
phonevarchar(15)YESNULL
admitteddateNONULL
4 rows in set
How this query works, step by step
  1. full_name and admitted are NOT NULL; the Null column of DESCRIBE shows NO.
  2. phone is optional (YES).
  3. A primary key is automatically NOT NULL.

Inserting without a required value

Leaving a NOT NULL column out:

MySQL
CREATE TABLE patients (patient_id INT PRIMARY KEY, full_name VARCHAR(60) NOT NULL, phone VARCHAR(15), admitted DATE NOT NULL);INSERT INTO patients (patient_id, full_name) VALUES (1, 'Ravi Shah');
ResultMySQL 8.4
mysql> CREATE TABLE patients (patient_id INT PRIMARY KEY, full_name VARCHAR(6…
Query OK, 0 rows affected
mysql> INSERT INTO patients (patient_id, full_name) VALUES (1, 'Ravi Shah')
ERROR 1364 (HY000): Field 'admitted' doesn't have a default value

Giving NULL explicitly:

MySQL
CREATE TABLE patients (patient_id INT PRIMARY KEY, full_name VARCHAR(60) NOT NULL, phone VARCHAR(15), admitted DATE NOT NULL);INSERT INTO patients VALUES (1, NULL, '98450 11111', '2026-01-10');
ResultMySQL 8.4
mysql> CREATE TABLE patients (patient_id INT PRIMARY KEY, full_name VARCHAR(6…
Query OK, 0 rows affected
mysql> INSERT INTO patients VALUES (1, NULL, '98450 11111', '2026-01-10')
ERROR 1048 (23000): Column 'full_name' cannot be null
How this query works, step by step
  1. Error 1364: the column was not given and has no default.
  2. Error 1048: an explicit NULL was given for a NOT NULL column.
  3. An UPDATE ... SET full_name = NULL fails with error 1048 in the same way.
Note

An empty string '' is not NULL, so NOT NULL does not stop it. Add a CHECK (full_name <> '') if empty text must be rejected too.

Advertisement

NOT NULL with DEFAULT

Combine them when a value is required but has an obvious standard:

SQL
CREATE TABLE tickets (  ticket_id INT PRIMARY KEY,  status    VARCHAR(10) NOT NULL DEFAULT 'open');INSERT INTO tickets (ticket_id) VALUES (1);SELECT * FROM tickets;
ResultMySQL 8.4
mysql> CREATE TABLE tickets (
Query OK, 0 rows affected
mysql> INSERT INTO tickets (ticket_id) VALUES (1)
Query OK, 1 row affected
mysql> SELECT * FROM tickets
ticket_idstatus
1open
1 row in set

Add NOT NULL to an existing column

MODIFY redefines the column. It fails if existing rows contain NULL:

MySQL
ALTER TABLE customers MODIFY email VARCHAR(60) NOT NULL;
ResultMySQL 8.4
ERROR 1138 (22004): Invalid use of NULL value

Fix the data first, then add the constraint:

MySQL
UPDATE customers SET email = 'unknown@example.com' WHERE email IS NULL;ALTER TABLE customers MODIFY email VARCHAR(60) NOT NULL;SELECT customer_id, email FROM customers WHERE customer_id = 4;
ResultMySQL 8.4
mysql> UPDATE customers SET email = 'unknown@example.com' WHERE email IS NULL
Query OK, 1 row affected
mysql> ALTER TABLE customers MODIFY email VARCHAR(60) NOT NULL
Query OK, 0 rows affected
mysql> SELECT customer_id, email FROM customers WHERE customer_id = 4
customer_idemail
4unknown@example.com
1 row in set
How this query works, step by step
  1. Customer 4 has no email; the UPDATE fills in a placeholder.
  2. Now no row is NULL, so MODIFY ... NOT NULL succeeds.
  3. MODIFY must repeat the full column type (VARCHAR(60)), or the type would change too.

Remove the constraint again by modifying the column without NOT NULL:

MySQL
ALTER TABLE customers MODIFY email VARCHAR(60) NULL;

Quick quiz

1. Does NOT NULL reject an empty string ”?

2. Why does ALTER TABLE ... MODIFY email ... NOT NULL fail?

Advertisement

NOT NULL FAQ

What does NOT NULL mean in SQL?

The column must always contain a value; inserts or updates that would leave it NULL are rejected.

How do I add a NOT NULL constraint to an existing column in MySQL?

Update existing NULLs to a value, then run ALTER TABLE t MODIFY col TYPE NOT NULL; repeating the column’s full type.

Is a primary key NOT NULL automatically?

Yes. Every primary key column is implicitly NOT NULL.