Quantcast
Viewing latest article 4
Browse Latest Browse All 4

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.

Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
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.

 

Regards

Ahmad Osama

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

Follow me on TwitterFollow me on FaceBook

The post Primary key not null in SQL Server appeared first on SQL Server Blogs, Events, Webcasts, Videos, SQL Server learning & education.


Viewing latest article 4
Browse Latest Browse All 4

Trending Articles