search

Home  >  Q&A  >  body text

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

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

天蓬老师天蓬老师2788 days ago699

reply all(9)I'll reply

  • 黄舟

    黄舟2017-04-17 11:04:31

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 11:04:31

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 11:04:31

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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:04:31

    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.

    reply
    0
  • 阿神

    阿神2017-04-17 11:04:31

    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"

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:04:31

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 11:04:31

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 11:04:31

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

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:04:31

    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.

    reply
    0
  • Cancelreply