Primary key not null in SQL Server

A Jr. Developer asked me why a primary key not null in SQL Server. A primary key uniquely identifies a row in a table and a NULL can’t identify any row. Moreover, a primary key is used to find rows in a table and is essential for indexing and maintaining data.

Let’s see what happens if we try to add a primary key constraint on a null able column.

1_Primary key are not null in SQL Server

As shown in above image, the SQL Server terminates with error when creating primary key on a null able column.

Let’s see the behavior when defining composite primary key.

   

2_Primary key are not null in SQL Server

The SQL Server again terminates with error. Thus all columns participating in a primary key should be not null.

 

Like us on FaceBook Join the fastest growing SQL Server group on FaceBook

   

Leave a Reply

Your email address will not be published.