MySQL主键字段添加NOT NULL有什么意义?

发布于 2024-09-14 16:48:54 字数 438 浏览 3 评论 0原文

NOT NULL 添加到主键字段有什么意义?主键已经不为空 + 唯一

这是一个例子:

CREATE TABLE student (
  id int(11) AUTO_INCREMENT NOT NULL,
  name varchar(255),
  PRIMARY KEY(id)
)

为什么不这样定义它:

CREATE TABLE student (
  id int(11) AUTO_INCREMENT,
  name varchar(255),
  PRIMARY KEY(id)
)

What's the point of adding NOT NULL to a primary key field? Primary key is already not null + unique.

Here is an example:

CREATE TABLE student (
  id int(11) AUTO_INCREMENT NOT NULL,
  name varchar(255),
  PRIMARY KEY(id)
)

Why not to define it like this instead:

CREATE TABLE student (
  id int(11) AUTO_INCREMENT,
  name varchar(255),
  PRIMARY KEY(id)
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方的巷 2024-09-21 16:48:54

他们是一样的。主键NOT NULL自动

They are the same. Primary key got NOT NULL automatically.

梦萦几度 2024-09-21 16:48:54

你问,为什么人们在不必要的时候还要添加 NOT NULL 呢?我想只是因为它是好的风格。并向读者明确说明。

You are asking, why do people bother adding the NOT NULL when it is unnecessary? Just because it is good style, I guess. And makes it explicit to the reader.

杀手六號 2024-09-21 16:48:54

NULL 不等于 NULL(因为 NULL 表示未知或不存在的值),因此您将被允许拥有多个具有 < code>NULL 作为 id,即使定义了主键/唯一约束,因此使用 NOT NULL。如果 MySql 甚至允许您在可为空的字段上定义主键的话。

此外,由于主键经常在其他表的外键中使用,因此拥有一个或多个 NULL 值是没有意义的。

NULL is not equivalent to NULL(as NULL indicates an unknown or absent value), so you will be permitted to have multiple records that have NULL for the id, even though there's a primary key / unique constraint defined, hence the use of NOT NULL. That's if MySql even allows you to define a primary key on a nullable field.

In addition, as a primary key is often used in a foreign key in other tables, having one or more NULL values wouldn't make sense.

〆凄凉。 2024-09-21 16:48:54

如果在声明 PRIMARY KEY 时未声明 NOT NULL,则 NOT NULL 会隐式添加到 PRIMARY KEY 中,因此 PRIMARY KEY 带或不带 NOT NULL 是一样的。

MySQL 文档如下图所示:

唯一索引,其中所有键列必须定义为 NOT NULL。如果
它们没有显式声明为 NOT NULL,MySQL 是这样声明的
隐含地(并且默默地)。一张表只能有一个主键。这
PRIMARY KEY 的名称始终是 PRIMARY,因此不能用作
任何其他类型索引的名称。

另外,MySQL 文档 如下图所示:

表的主键表示列或列集
您在最重要的查询中使用的。它有一个关联的索引,
以获得快速查询性能。查询性能受益于 NOT
NULL 优化,因为它不能包含任何 NULL 值。 ...

If PRIMARY KEY is declared without NOT NULL, NOT NULL is added to PRIMARY KEY implicitly so PRIMARY KEY with or without NOT NULL is the same.

The MySQL documentation says as shown below:

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.

And also, the MySQL documentation says as shown below:

The primary key for a table represents the column or set of columns
that you use in your most vital queries. It has an associated index,
for fast query performance. Query performance benefits from the NOT
NULL optimization, because it cannot include any NULL values. ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文