The following code creates a table without raising any errors:
CREATE TABLE test(
ID INTEGER NULL,
CONSTRAINT PK_test PRIMARY KEY(ID)
)
Note that I cannot insert a NULL, as expected:
INSERT INTO test
VALUES(1),(NULL)
ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null).
********** Error **********
ERROR: null value in column "id" violates not-null constraint
SQL state: 23502
Detail: Failing row contains (null).
Why can I create a table with a self-contradictory definition? ID column is explicitly declared as NULLable, and it is implicitly not nullable, as a part of the PRIMARY KEY. Does it make sense?
Edit: would it not be better if this self-contradictory CREATE TABLE just failed right there?
If as @ErwinBrandstetter said, PRIMARY KEY is merely a combination of UNIQUE and NOT NULL, you can use an
UNIQUE
constraint withoutNOT NULL
instead ofPRIMARY KEY
. Example:This way you can do things like:
If memory serves, the docs mention that:
null
in create table statements is basically a noise word that gets ignoredprimary key
forces a not null and a unique constraintSee:
Because the
PRIMARY KEY
makes the columnNOT NULL
automatically. I quote the manual here:Bold emphasis mine.
I ran a test to confirm that (against my former belief!)
NOT NULL
is completely redundant in combination with aPRIMARY KEY
constraint (in the current implementation, up to version 9.5). The NOT NULL constraint stays after you drop the PK constraint, irregardless of an explicitNOT NULL
clause at creation time.Identical behaviour if
NULL
is included in theCREATE
statement.However, it still won't hurt to keep
NOT NULL
redundantly in code repositories if the column is supposed to beNOT NULL
. If you later decide to move the pk constraint around, you might forget to mark the columnNOT NULL
- or whether it even was supposed to beNOT NULL
.There is an item in the Postgres TODO wiki to decouple
NOT NULL
from the PK constraint. So this might change in future versions:Answer to added question:
As explained above, this
is equivalent to:
Since
NULL
is treated as noise word.And we wouldn't want the latter to fail. So this is not an option.