kernanb
kernanb

Reputation: 586

SQL Server - 'NOT NULL' Constraint redundant on Primary Key?

In the below SQL code is 'NOT NULL' redundant since f_ID is a primary key and thus can't be null for any record?

CREATE TABLE t_Activities(
    f_ID varchar(50) NOT NULL,
    PRIMARY KEY (f_ID))

Upvotes: 4

Views: 2316

Answers (3)

user596075
user596075

Reputation:

That is correct. If you had:

CREATE TABLE t_Activities( 
    f_ID varchar(50), 
    PRIMARY KEY (f_ID)) 

It would be the equivalen. The Primary Key constraint does not allow NULLs.

Upvotes: 1

Remus Rusanu
Remus Rusanu

Reputation: 294217

What if you later drop the primary key constraint? Should the column allow nulls or not after that? Yes, no, maybe? The two constraints are different. PK constraint requires a NOT NULL, but does not implies one.

Upvotes: 4

Adam Wenger
Adam Wenger

Reputation: 17540

It may be redundant, but I prefer this method of creating a primary key (or Identity column). It shows that the person making the table understands and intends the column to be NOT NULL as well as the Primary Key for the table.

Upvotes: 1

Related Questions