mysql - 数据库中,NULL与空值有什么区别
天蓬老师
天蓬老师 2017-04-17 11:02:31
0
9
694

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(9)
黄舟

A NULL value indicates an unknown value. A null value is different from a blank or zero value. No two null values ​​are equal. Comparing two null values ​​or comparing a null value to any other value returns unknown because each null value is unknown.

When writing data, the empty string '' is also a certain value, so it can be written even if you define NOT NULL.

大家讲道理

NULL is a virgin
''Not a virgin, but currently single

巴扎黑

The difference between NULL and empty value is basically the difference between 0 and "none".

左手右手慢动作

In MySQL, for each field that is allowed to be NULL, mysql must reserve a bit of information for it (in each row) to mark whether it is NULL.

阿神

In the situation you mentioned:
NULL means unknown
An empty string represents a known empty

Please note that the exact meaning of NULL is "unknown" rather than "empty"

左手右手慢动作

"" In fact, some people know. For example: For example, air actually exists, and NULL can be said to be a vacuum.

洪涛

以下是摘自 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.
Peter_Zhu

NULL is an uncertain content. A null value is equivalent to ""

黄舟

The difference between null and '' is like a vacuum and a space with air. null means nothing in the pure sense, '' like a zero-length string.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template