SQL -Structured Query Language - Sql Key & Constraint - Not Null Constraint Tutorial
Not Null Constraint is used, when we don't want to keep the null value in a specific column of a table.
After creating a table with a not null constraint, it doesn’t allow to insert an empty value.
By default, the column can hold a null value.
The syntax for Not Null Constraint-
create table user_detail
(
id int,
email varchar(50) not null
);
In this, we define email with not null constraints while creating a user_detail table. With a not null constraint, it doesn’t allow to insert an empty value.
It will give an error (NOT NULL constraint failed: user_detail.email) while inserting the empty or null value.
Syntax For Adding NOT NULL Constraint With Alter Command-
ALTER TABLE user_detail
MODIFY email varchar(50) NOT NULL;
This syntax will add a NOT NULL in an email after table creation. If it gives an error make sure that records should not contain null values already.