What's wrong with nullable columns in composite primary keys?

后端 未结 6 1004
囚心锁ツ
囚心锁ツ 2020-11-27 12:57

ORACLE does not permit NULL values in any of the columns that comprise a primary key. It appears that the same is true of most other \"enterprise-level\" systems.

At

6条回答
  •  攒了一身酷
    2020-11-27 13:08

    Primary keys are for uniquely identifying rows. This is done by comparing all parts of a key to the input.

    Per definition, NULL cannot be part of a successful comparison. Even a comparison to itself (NULL = NULL) will fail. This means a key containing NULL would not work.

    Additonally, NULL is allowed in a foreign key, to mark an optional relationship.(*) Allowing it in the PK as well would break this.


    (*)A word of caution: Having nullable foreign keys is not clean relational database design.

    If there are two entities A and B where A can optionally be related to B, the clean solution is to create a resolution table (let's say AB). That table would link A with B: If there is a relationship then it would contain a record, if there isn't then it would not.

提交回复
热议问题