首页 > 数据库中,NULL与空值有什么区别

数据库中,NULL与空值有什么区别

创建数据库的时候,对某个字段定义了 NOT NULL,但是,在写入数据的时候,空字符串''竟然也能写入成功。
有人能给我解释一下 NULL 和 NOT NULL 到底是什么意思呢?


在你说的情况中:
NULL 表示 未知
空字符串表示 已知为 空

要注意NULL的确切意思是 "未知" 而不是 "空"


NULL是不确定的内容,空值的话是相当于""这个吧


NULL跟空值的区别,基本上就是0跟“没有”的区别。


"" 其实有知道的, 举个例子: 例如空气他其实是存在的, 而NULL 可以说是真空。


空 (NULL) 值表示数值未知。空值不同于空白或零值。没有两个相等的空值。比较两个空值或将空值与任何其它数值相比均返回未知,这是因为每个空值均为未知。

在写入数据的时候,空字符串'' 也是一个确定的值,所以就算你定义了 NOT NULL 也可以被写入。


以下是摘自 Oracle SQL Reference 的内容,供你参考。版本是11g Release 2。

If a column in a row has no value, then the column is said to be null, or to contain null. Nulls can appear in columns of any data type that are not restricted by NOT NULL or PRIMARY KEY integrity constraints. Use a null when the actual value is not known or when a value would not be meaningful. Oracle Database treats a character value with a length of zero as null. However, do not use null to represent a numeric value of zero, because they are not equivalent.
Note: Oracle Database currently treats a character value with a length of zero as null. However, this may not continue to be true in future releases, and Oracle recommends that you do not treat empty strings the same as nulls.
Any arithmetic expression containing a null always evaluates to null. For example, null added to 10 is null. In fact, all operators (except concatenation) return null when given a null operand.

null和''的区别,就像真空和一个存在空气的空间一样。null是纯粹意义上的没有任何东西,''就像一个长度为零的字符串。


在MySQL中,对于每一个允许为NULL的字段,mysql要(在每一行)为它预留一个bit的信息,用来标记它是否为NULL。


NULL是处女
''是非处女,但目前单身

【热门文章】
【热门文章】