MySQL - Are “NOT NULL” constraints needed for primary keys?

前端 未结 4 604
轮回少年
轮回少年 2021-01-04 04:15

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 auto

4条回答
  •  既然无缘
    2021-01-04 04:39

    (As you've tagged your question mysql.) In MySQL, you don't have to do it explicitly. From the manual:

    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).

    Of course, just because you don't have to doesn't mean you might not want to for clarity, etc.

提交回复
热议问题