Mysql:允许Null含义
“允许 Null”
是什么意思,它有什么作用?
What does "Allow Null"
mean, and what does it do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
“允许 Null”
是什么意思,它有什么作用?
What does "Allow Null"
mean, and what does it do?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
为了理解“允许 Null”的含义,您需要了解
NULL
是什么,并了解它与简单的零或空白字符串不同。NULL
是 SQL 中的一个特殊值。表示数据不存在。这与空白数据不同。当您在MySQL中创建表时,它允许您指定字段是否允许为
NULL
。如果您指定“允许 Null”,则可以在这些字段中创建具有 NULL 值的记录。在某些情况下这可能对您有用。例如,“是/否”字段可能是布尔值,但如果用户未指定其首选项,您可能需要设置为
NULL
来指示这一点,而不是默认为“是”或“否”。但在许多情况下,允许 NULL 可能会出现问题。设置为 NULL 的字段可能会在查询中产生意外结果,因为 NULL 并不在所有情况下都遵循与其他值相同的规则。例如:如果您查询上面的示例字段
WHERE myfield != 1
,您可能期望获得所有不等于 1 的记录的结果。但是NULL
字段将不予退回;你需要为他们写一个特殊的案例。在执行
JOIN
查询(其中某些结果没有要连接的记录)时,NULL
也用作默认值。连接表中的未知字段将全部设置为NULL
。一般来说,如果您不确定如何处理“允许空值”,最安全的选择是将其设置为不允许空值。
有关详细信息,请参阅SQL 中 NULL 的维基百科条目。
希望有帮助。
In order to understand what "Allow Null" means, you need to understand what
NULL
is, and understand that it is different to simply a zero or blank string.NULL
is a special value in SQL. It indicates data that does not exist. This is different to data that is blank.When you create a table in MySQL, it allows you to specify whether your fields are allowed to be
NULL
. If you specify "Allow Null", then it will be possible to create records withNULL
values in those fields.This may be useful to you in some cases. For example, a Yes/No field may be a boolean value, but if the user didn't specify their preference, you may want to set to
NULL
to indicate this rather than defaulting to yes or no.In many cases though, allowing
NULL
can be problematic. Fields set toNULL
can produce unexpected results in queries, becauseNULL
does not follow the same rules as other values in all cases. eg: If you query the above example fieldWHERE myfield != 1
, you may expect to get results for all the records which are not equal to 1. HoweverNULL
fields will not be returned; you need to write a special case for them.NULL
is also used as the default value when doingJOIN
queries where some of the results do not have a record to join to. The unknown fields from the joined table will all be set toNULL
.In general, if you're not sure what to do with "Allow Null", the safest option is to set it to no allow nulls.
See the Wikipedia entry for NULL in SQL for more info.
Hope that helps.
允许空值意味着当您更改或插入数据时,您不需要填充该数据。这不是强制性的。
Allow Null means that when you change or insert the data you dont need to fill that data in. Its not compulsory.
本质上,如果您允许空值,则表示相关字段可以包含合法值(例如:2011-04-02,如果它是日期)以及“NULL”(或“无值”) 。
因此,如果您想象某个字段是必填字段的情况,则应始终在架构声明中将其表示为
NOT NULL
,而可选字段则可以保留原样。有关详细信息,请查看使用 NULL 值 部分,其中(除其他外)巧妙地总结了以下内容:
In essence, if you allow a null value you're saying that the field in question can contain a legitimate value (e.g.: 2011-04-02 if it was a date) as well as "NULL" (or "no value").
As such, if you imagine a situation where a field was mandatory, it should always be denoted as
NOT NULL
in the schema declaration, whereas an optional field can be left as is.For more information, check out the Working with NULL Values section of the MySQL manual, which (amongst other things) neatly summarises things thus: