SQL NOT NULLConstraints


By default, table columns accept NULL values.


SQL NOT NULL Constraint

The NOT NULL constraint enforces a column to not accept NULL values.

The NOT NULL constraint forces 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 this field.

The following SQL ensures that the "ID", "LastName", and "FirstName" columns do not accept NULL values:

Examples

CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int );

Add NOT NULL Constraint

To add a NOT NULL constraint to the "Age" column in an already created table, use the following:

Examples

ALTER TABLE Persons MODIFY Age int NOT NULL;

Drop NOT NULL Constraint

To drop a NOT NULL constraint from the "Age" column in an already created table, use the following:

Examples

ALTER TABLE Persons MODIFY Age int NULL;
Other Extensions