MySQL - 是“NOT NULL”主键所需的约束? [英] MySQL - Are "NOT NULL" constraints needed for primary keys?

查看:239
本文介绍了MySQL - 是“NOT NULL”主键所需的约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有必要在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 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.

这篇关于MySQL - 是“NOT NULL”主键所需的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆