NULL value in multi-column primary key

前端 未结 5 1358
执笔经年
执笔经年 2020-12-29 17:54

I\'ve got a table with several columns making up the primary key. The nature of the data stored allows some of these fields to have NULL values. I have designed

5条回答
  •  爱一瞬间的悲伤
    2020-12-29 18:11

    From the MySQL documentation :

    A PRIMARY KEY is a unique index where all key columns must be defined as NOT NULL. If they
    are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one PRIMARY KEY. The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.

    http://dev.mysql.com/doc/refman/5.1/en/create-table.html

    If Field2 can be NULL, I question why you need it as part of the Primary Key since you then need Field1 to be distinct across all rows. So Field1 by itself should be sufficient as the Primary Key. You could create a different type of index on Field2.

提交回复
热议问题