Why can I create a table with PRIMARY KEY on a nullable column?

Posted by AlexKuznetsov on Stack Overflow See other posts from Stack Overflow or by AlexKuznetsov
Published on 2013-11-15T16:54:16Z Indexed on 2014/08/19 22:20 UTC
Read the original article Hit count: 176

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?

© Stack Overflow or respective owner

Related posts about postgresql

Related posts about constraints