d-_-b
d-_-b

Reputation: 23161

MySQL Unique vs Primary key in relation to NULL values

I've noticed something whilst setting up a database and want to confirm with all you smart people!

I have a column that needs to accept NULL values, so I checked off the NULL checkbox in the easy to use PHPMyAdmin interface. easy.

When I add a unique index, everything is good. But, when I change that unique index to a Primary key (also unique), it removes the NULL option.

Why is this, and is this expected behavior?

Upvotes: 0

Views: 108

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42074

That is expected. As told in manual, PRIMARY KEY cannot accept NULL:

A PRIMARY KEY is a unique index where all key columns must be defined as NOT NULL.

Upvotes: 2

Related Questions