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.
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.
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 Twitter | Follow 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.