Loading lesson path
Overview
constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field. By default, a column can hold NULL values.
constraint when creating a table, add NOT NULL after the data type of the column name. The following SQL creates a "Persons" table, and ensures that the "ID", "LastName", and "FirstName" columns cannot accept NULL values:
CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL,
NOT NULL,
);constraint on an existing table, use
and add NOT NULL after the data type of the column name.
constraint on the "Age" column, after the "Persons" table is already created:
Formula
Syntax for SQL Server / MS Access:ALTER COLUMN Age int NOT NULL;MODIFY COLUMN Age int NOT NULL;MODIFY Age int NOT NULL;constraint from a column (to let the column accept NULL values again), use the following syntax:Formula
Syntax for SQL Server / MS Access:ALTER COLUMN Age int NULL;MODIFY COLUMN Age int NULL;MODIFY Age int NULL;