MySQL - 是“NOT NULL”主键所需的约束? [英] MySQL - Are "NOT NULL" constraints needed for primary keys?
问题描述
是否有必要在MySQL数据库中声明NOT NULL约束的主键?主键不能具有NULL值,因为它仍然是auto_increments,并自动填充字段记录。
is it necessary to declare "NOT NULL" constraints for primary keys in the MySQL database? A primary key cannot have NULL values because it auto_increments anyway and automatically fills the field record. So am I correct in saying this mean I can remove the "NOT NULL" constraint for my primary keys?
推荐答案
是什么意思? (因为你已经标记你的问题 mysql
。)在MySQL中,你不必明确做。从手册:
A
PRIMARY KEY
是唯一索引,其中所有键列必须定义为NOT NULL
。如果它们没有明确声明为NOT NULL
,MySQL会隐式声明它们(并且静默)。
A
PRIMARY KEY
is a unique index where all key columns must be defined asNOT NULL
. If they are not explicitly declared asNOT NULL
, MySQL declares them so implicitly (and silently).
当然,只是因为你不有不意味着你可能不想为了清楚起见等。
Of course, just because you don't have to doesn't mean you might not want to for clarity, etc.
这篇关于MySQL - 是“NOT NULL”主键所需的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!