NOT NULL specifies that NULL values are not accepted in a column.
This constraint ensures that invalid data cannot enter the column.
Adding a NULL value to a NOT NULL column will cause an error.
In this table, the FirstName and LastName columns do not accepts NULL values.
CREATE TABLE Customer (
Id INT IDENTITY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
City NVARCHAR(40),
CONSTRAINT PK_Customer PRIMARY KEY(Id)
)
Note: The City column does accept NULL values.
To add a NOT NULL
constraint to an existing column use ALTER TABLE
.
ALTER TABLE Customer
ALTER COLUMN City NVARCHAR(40) NOT NULL