Showing posts with label Constraint NOT NULL. Show all posts
Showing posts with label Constraint NOT NULL. Show all posts

Friday, February 22, 2019

How to create SQL NotNULL constraint

SQL Not NULL constraint ensures the columns cannot have NULL value, if you insert or update NULL value to this column, it throws exception

Cannot insert the value NULL into column 'StatusCode', table 'dbo.OrderStatus'; column does not allow nulls. INSERT fails.


Create NOT NULL constraint:

SQL constrain NOT NULL can be defined in create table script

Create Table OrderStatus
(
       StatusID int IDENTITY(1,1) NOT NULL,
       StatusCode varchar(3) NOT NULL,
       StatusDescription varchar(50),
       CONSTRAINT [OrderStatus_PK] PRIMARY KEY CLUSTERED ( StatusID ASC),  
)

In above, we define NOT NULL constraint for StatusCode Columns.