SQL NOT NULL
Constraint
By default, columns in a table accept NULL values.
SQL NOT NULL Constraint
The NOT NULL constraint enforces a column to not accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record or update a record without adding a value to the field.
The following SQL enforces the "ID" column, "LastName" column, and "FirstName" column to not accept NULL values:
Example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Adding NOT NULL Constraint
To add a NOT NULL constraint to the "Age" field of an already created table, use the following:
Example
ALTER TABLE Persons
MODIFY Age int NOT NULL;
Removing NOT NULL Constraint
To remove a NOT NULL constraint from the "Age" field of an already created table, use the following:
Example
ALTER TABLE Persons
MODIFY Age int NULL;