SQL Server: NOT NULL Constraint

Introduction:

In this article i will explain what is NOT NULL Constraint in SQL Server.

Description:

In previous articles i explained what is Constraint. And how many Constraints available in SQL Server. Now i will explain what is NOT NULL Constraint in SQL Server.

NOT NULL Constraint:

NOT NULL specifies that the column does not accept NULL values.

Example:

Create table Employee
(
    EmpNo int not null,
    EmpName varchar(50) not null
)

Employee table is created.

Insert data into Employee table.

insert into Employee values(1, 'AAA')

(1 row(s) affected)

1 row inserted into Employee table.

insert into Employee(EmpNo) values(2)

After executing the above statement we got the following error. Because EmpName column does not allow null value.

Cannot insert the value NULL into column 'EmpName', table 'Test.dbo.Employee'; column does not allow nulls. INSERT fails.