You are currently viewing How To Add NOT NULL Constraint in SQL Server

How To Add NOT NULL Constraint in SQL Server

A NOT NULL constraint on a column does not allow to enter a NULL value. 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, every column can hold a NULL value.

NOT NULL Constraint on CREATE TABLE

In the below table tblEmployee EmpID, EmpFirstName, EmpLastName column values are mandatory because of NOT NULL constraint.

The EmpID column is a PRIMARY KEY column which does not accept NULL values and EmpFirstName and EmpLastName has NOT NULL constraints, therefore these columns does not accept NULL values.

CREATE TABLE tblEmployee
(

EmpID INT PRIMARY KEY,
EmpFirstName NVARCHAR(20) NOT NULL,
EmpLastName NVARCHAR(20) NOT NULL,
EmpEmail NVARCHAR(20),
EmpPhone NVARCHAR(15),
EmpDeptID INT,
EmpSalary INT,
EmpJoiningDate DATETIME

)

NOT NULL Constraint on ALTER TABLE

ALTER TABLE tblEmployee
MODIFY EmpSalary INT NOT NULL

Now, If I try to insert NULL value for EmpLastName, I will get an error as shown in the below image.

Recommended Readings

Buy Best Stationery Products in Pakistan

Piano Ballpoint Pen

Piano Ballpoint Pen

ORO Trimetal Pencil

ORO Trimetal

Dollar Clipper Pen

UHU 10×7 ml Pack of 10 PCs

UHU 10×7 ml Pack of 10 PCs

Glue Stick 8g Pack Of 12 PCs

Glue Stick 8g

Color Pencils Box of 12 PCs

Deer Multi Color Pencils