-1

A piece of sql in sql server

CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

Primary key is not null by nature, why we still specify not null here? Do I miss anything?

ZZZ
  • 3,574
  • 10
  • 34
  • 37

4 Answers4

4

Yes, "primary key" is "not null", by definition.

Q: Do you object to typing in eight extra characters?

Q: Have you tried omitting "not null"? What happens?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
2

You don't have to use it, it's redundant...

gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Because Primary key will not contain NULL value. This key is the Identifier of the records.

John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Because it is copy-paste from auto-generated script, and Not Null is coming from column settings, and primary key from constraint definition?

Val Bakhtin
  • 1,434
  • 9
  • 11