Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of adding NOT NULL to primary key field in MySQL?

What's the point of adding NOT NULL to a primary key field? Primary key is already not null + unique.

Here is an example:

CREATE TABLE student (
  id int(11) AUTO_INCREMENT NOT NULL,
  name varchar(255),
  PRIMARY KEY(id)
)

Why not to define it like this instead:

CREATE TABLE student (
  id int(11) AUTO_INCREMENT,
  name varchar(255),
  PRIMARY KEY(id)
)
like image 322
Ben Avatar asked Aug 17 '10 13:08

Ben


2 Answers

They are the same. Primary key got NOT NULL automatically.

like image 73
J-16 SDiZ Avatar answered Oct 01 '22 04:10

J-16 SDiZ


You are asking, why do people bother adding the NOT NULL when it is unnecessary? Just because it is good style, I guess. And makes it explicit to the reader.

like image 20
Hammerite Avatar answered Oct 01 '22 03:10

Hammerite