简体   繁体   English

NULL如何存储在数据库中?

[英]How are NULLs stored in a database?

I'm curious to know how NULLs are stored into a database ? 我很想知道如何将NULL存储到数据库中?

It surely depends on the database server but I would like to have an general idea about it. 它肯定取决于数据库服务器,但我想对它有一个大致的想法。


First try: 第一次尝试:

Suppose that the server put a undefined value (could be anything) into the field for a NULL value. 假设服务器将未定义的值(可能是任何值)放入字段中以获取NULL值。

Could you be very lucky and retrieve the NULL value with 你能非常幸运并用它来检索NULL值吗?

...WHERE field = 'the undefined value (remember, could be anything...)'

Second try: 第二次尝试:

Does the server have a flag or any meta-data somewhere to indicate this field is NULL ? 服务器是否有某个标志或任何元数据表明此字段为NULL?

Then the server must read this meta data to verify the field. 然后,服务器必须读取此元数据以验证该字段。

If the meta-data indicates a NULL value and if the query doesn't have "field IS NULL", then the record is ignored. 如果元数据指示NULL值,并且查询没有“field IS NULL”,则忽略该记录。


It seems too easy... 看起来太容易了......

MySql uses the second method. MySql使用第二种方法。 It stores an array of bits (one per column) with the data for each row to indicate which columns are null and then leaves the data for that field blank. 它存储一个位数组(每列一个),每行包含数据,以指示哪些列为空,然后将该字段的数据留空。 I'm pretty sure this is true for all other databases as well. 我很确定这对所有其他数据库也是如此。

The problem with the first method is, are you sure that whatever value you select for your data won't show up as valid data? 第一种方法的问题是,您确定为数据选择的任何值都不会显示为有效数据吗? For some values (like dates, or floating point numbers) this is true. 对于某些值(如日期或浮点数),这是真的。 For others (like integers) this is false. 对于其他人(如整数),这是错误的。

On PostgreSQL, it uses an optional bitmap with one bit per column (0 is null, 1 is not null). 在PostgreSQL上,它使用一个可选的位图,每列一位(0为空,1不为空)。 If the bitmap is not present, all columns are not null. 如果位图不存在,则所有列都不为空。

This is completely separate from the storage of the data itself, but is on the same page as the row (so both the row and the bitmap are read together). 这与数据本身的存储完全分开,但与行位于同一页面上(因此行和位图一起读取)。

References: 参考文献:

The server typically uses meta information rather than a magic value. 服务器通常使用元信息而不是魔术值。 So there's a bit off someplace that specifies whether the field is null. 所以有一些地方指出该字段是否为空。

-Adam -亚当

IBM Informix Dynamic Server uses special values to indicate nulls. IBM Informix Dynamic Server使用特殊值来指示空值。 For example, the valid range of values for a SMALLINT (16-bit, signed) is -32767..+32767. 例如,SMALLINT(16位,有符号)的有效值范围是-32767 .. + 32767。 The other value, -32768, is reserved to indicate NULL. 另一个值-32768保留为表示NULL。 Similarly for INTEGER (4-byte, signed) and BIGINT (8-byte, signed). 类似地,对于INTEGER(4字节,有符号)和BIGINT(8字节,有符号)。 For other types, it uses other special representations (for example, all bits 1 for SQL FLOAT and SMALLFLOAT - aka C double and float, respectively). 对于其他类型,它使用其他特殊表示(例如,SQL FLOAT和SMALLFLOAT的所有位1 - 分别为C double和float)。 This means that it doesn't have to use extra space. 这意味着它不必使用额外的空间。

IBM DB2 for Linux, Unix, Windows uses extra bytes to store the null indicators; IBM DB2 for Linux,Unix,Windows使用额外的字节来存储空指示符; AFAIK, it uses a separate byte for each nullable field, but I could be wrong on that detail. AFAIK,它为每个可空字段使用一个单独的字节,但我可能在这个细节上错了。

So, as was pointed out, the mechanisms differ depending on the DBMS. 因此,正如所指出的,机制因DBMS而异。

The problem with special values to indicate NULL is that sooner or later that special value will be inserted. 指示NULL的特殊值的问题是迟早会插入特殊值。 For example, it will be inserted into a table specifying the special NULL indicators for different database servers 例如,它将插入到表中,为不同的数据库服务器指定特殊的NULL指示符

| DBServer     | SpecialValue |
+--------------+--------------+
| 'Oracle'     | 'Glyph'      |
| 'SQL Server' | 'Redmond'    |

;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM