Mysql:允许Null的含义 [英] Mysql: Allow Null meaning

查看:198
本文介绍了Mysql:允许Null的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"Allow Null"是什么意思?

推荐答案

为了理解允许空值"的含义,您需要了解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是SQL中的特殊值.它指示不存在的数据.这与空白数据不同.

NULL is a special value in SQL. It indicates data that does not exist. This is different to data that is blank.

在MySQL中创建表时,它允许您指定是否允许您的字段为NULL.如果指定允许为空",则可以在这些字段中使用NULL值创建记录.

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 with NULL values in those fields.

在某些情况下,这可能对您有用.例如,是/否"字段可能是一个布尔值,但是如果用户未指定其偏好设置,则可能需要设置为NULL来表明这一点,而不是默认为是或否.

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.

在许多情况下,允许NULL可能会出现问题.设置为NULL的字段可能会在查询中产生意外结果,因为NULL并非在所有情况下都遵循与其他值相同的规则.例如:如果查询上面的示例字段WHERE myfield != 1,则可能希望获得所有不等于1的记录的结果.您需要为他们写一个特殊情况.

In many cases though, allowing NULL can be problematic. Fields set to NULL can produce unexpected results in queries, because NULL does not follow the same rules as other values in all cases. eg: If you query the above example field WHERE myfield != 1, you may expect to get results for all the records which are not equal to 1. However NULL fields will not be returned; you need to write a special case for them.

NULL也用作默认值.联接表中的未知字段将全部设置为NULL.

NULL is also used as the default value when doing JOIN 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 to NULL.

通常,如果不确定如何使用允许空值",最安全的选择是将其设置为不允许空值.

In general, if you're not sure what to do with "Allow Null", the safest option is to set it to no allow nulls.

有关更多信息,请参见 SQL中NULL的维基百科条目.

See the Wikipedia entry for NULL in SQL for more info.

希望有帮助.

这篇关于Mysql:允许Null的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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