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

前端 未结 3 574
后悔当初
后悔当初 2020-12-11 00:00

The following code creates a table without raising any errors:

CREATE TABLE test(
ID INTEGER NULL,
CONSTRAINT PK_test PRIMARY KEY(ID)
)

Not

3条回答
  •  春和景丽
    2020-12-11 00:35

    If as @ErwinBrandstetter said, PRIMARY KEY is merely a combination of UNIQUE and NOT NULL, you can use an UNIQUE constraint without NOT NULL instead of PRIMARY KEY. Example:

    CREATE TABLE test(
        id integer,
        CONSTRAINT test_id_key UNIQUE(id)
    );
    

    This way you can do things like:

    INSERT INTO test (id) VALUES (NULL);
    INSERT INTO test (id) VALUES (NULL);
    INSERT INTO test (id) VALUES (NULL);
    

提交回复
热议问题