SQL NOT NULL Keyword
The NOT NULL constraint ensures that a column cannot have a NULL value. This means that the column must always contain a value when a record is inserted or updated.
SQL NOT NULL Syntax
CREATE TABLE table_name ( column1 datatype NOT NULL, column2 datatype, ... );
To add a NOT NULL
constraint to an existing column in some databases:
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
Note: The MODIFY syntax may vary depending on the SQL engine.
SQL NOT NULL on CREATE TABLE Example
This query creates a new table DoctorsBackup where the first_name and last_name columns must have values.
CREATE TABLE DoctorsBackup ( doctor_id INTEGER PRIMARY KEY, first_name TEXT NOT NULL, last_name TEXT NOT NULL );

SQL NOT NULL on Existing Table Example
This query alters the Students table to ensure the email column cannot have NULL values.
-- Not supported in SQLite: ALTER TABLE Students MODIFY email TEXT NOT NULL;
Note: This example does not work in our TechKubo playground (SQLite), because SQLite does not support modifying existing columns to add NOT NULL directly.
Tip: Always use
NOT NULL
on columns where a missing value would break your application logic or data integrity.