SQL Server 中的 NULL 值是什么?

发布于 2024-09-04 22:53:17 字数 57 浏览 4 评论 0原文

我知道 NULL 不是零...也不是空字符串。但是系统保留的 NULL... 的值是多少来识别它呢?

I know NULL is not zero... nor it is empty string. But then what is the value of NULL... which the system keeps, to identify it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

白龙吟 2024-09-11 22:53:17

NULL 是 SQL 语言的一个特殊元素,它既不等于也不不等于任何数据类型中的任何值。

正如您所说,NULL 不是零、空字符串或 false。即 false = NULL 返回 UNKNOWN

有人说 NULL 不是一个值,而是一个状态。没有价值的状态。有点像禅宗公案。 :-)

我不知道MS SQL Server内部具体是如何存储的,但这并不重要,只要他们按照SQL标准实现即可。

NULL is a special element of the SQL language that is neither equal to or unequal to any value in any data type.

As you said, NULL is not zero, an empty string, or false. I.e. false = NULL returns UNKNOWN.

Some people say NULL is not a value, it's a state. The state of having no value. Sort of like a Zen Koan. :-)

I don't know specifically how MS SQL Server stores it internally, but it doesn't matter, as long as they implement it according to the SQL standard.

自找没趣 2024-09-11 22:53:17

我相信对于允许空值的每一列,行都有一个空位图。如果指定列中的行为空,则位图中的位为 1,否则为 0。

I believe that for each column that allow nulls, the rows have a null bitmap. If the row in the specified column is null the bit in the bitmap is 1 otherwise is 0.

鸢与 2024-09-11 22:53:17

SQL Server 行格式在 MSDN 中进行了描述,并在各种博客上进行了分析,例如 Paul Randal 的 记录剖析。重要的信息是记录结构:

  • 记录标题
    • 4 个字节长
    • 两个字节的记录元数据(记录类型)
    • 记录中向前指向 NULL 位图的两个字节
  • 记录的固定长度部分,包含存储固定长度数据类型的列(例如bigint、char(10)、datetime)
  • NULL 位图
    • 两个字节用于记录中的列数
    • 记录中每列存储一位的可变字节数,无论该列是否可为空
    • 这允许在读取 NULL 列时进行优化
  • 可变长度列偏移数组
    • 两个字节用于可变长度列的计数
    • 每个可变长度列两个字节,给出列值末尾的偏移量
  • 版本控制标签
    • 一个 14 字节结构,其中包含时间戳以及指向 tempdb 中版本存储的指针

因此 NULL 字段在 NULL 位图中设置了一个位。

The SQL Server row format is described in MSDN, and also analyzed on various blogs, like Paul Randal's Anatomy of a Record. The important information is the record structure:

  • record header
    • 4 bytes long
    • two bytes of record metadata (record type)
    • two bytes pointing forward in the record to the NULL bitmap
  • fixed length portion of the record, containing the columns storing data types that have fixed lengths (e.g. bigint, char(10), datetime)
  • NULL bitmap
    • two bytes for count of columns in the record
    • variable number of bytes to store one bit per column in the record, regardless of whether the column is nullable or not
    • this allows an optimization when reading columns that are NULL
  • variable-length column offset array
    • two bytes for the count of variable-length columns
    • two bytes per variable length column, giving the offset to the end of the column value
  • versioning tag
    • a 14-byte structure that contains a timestamp plus a pointer into the version store in tempdb

So NULL fields have a bit set in the NULL bitmap.

揽月 2024-09-11 22:53:17

我怀疑面试官想让你确切地知道 SQL Server 如何存储 null,这样的问题的重点是让你思考如何存储特殊值。您不能使用哨兵值(或幻数),因为这会使包含该值的任何行突然变为 null

有很多方法可以实现这一目标。我想到的最简单的 2 种方法是为每个可空值存储一个标志,该标志基本上是一个 isNull 标志(这也是 Nullable 的工作原理) 。网)。第二种方法是为每一行存储一个空标志位图,每一列一个。

当面对这样的面试问题时,最糟糕的反应就是坐着发呆。大声思考,承认您不知道 SQL Server 是如何做到这一点的,然后提出一些听起来合理的方法。您还应该准备好谈论为什么选择一种方法而不是另一种方法,以及每种方法的优点和缺点。

I doubt the interviewers wanted you to know exactly how SQL server stores nulls, the point of such a question is to get you to think about how you'd store special values. You can't use a sentinel value (or magic number), as that would make any rows with that value in them suddenly become null.

There are quite a few ways to achieve this. The most straightforward 2 that come to mind are to have a flag stored with each nullable value that is basically an isNull flag (this is also basically how Nullable<T> works in .NET). A 2nd method is to store with each row a bitmap of null flags, one for each column.

When faced with such an interview questions the absolute worst response is to sit and stare blankly. Think out loud some, admit that you don't know how SQL Server does it, and then present some reasonable sounding ways of doing it. You should also be ready to talk a bit about why you'd pick one method over another, and what the pluses and minuses of each are.

被你宠の有点坏 2024-09-11 22:53:17

正如比尔所说,这是一种状态而不是价值。

在 SQL Server 表的一行中,它存储在空位图中:实际上没有存储任何值。

SQL Server 的一个怪癖:

SELECT TOP 1 NULL AS foo INTO dbo.bar FROM sys.columns

foo 是什么数据类型?这当然没有什么意义,但有一次它让我感到困惑。

As Bill said, it's a state not a value.

In a row in a table SQL Server it's stored in the null bitmap: no value is actually stored.

One quirk of SQL Server:

SELECT TOP 1 NULL AS foo INTO dbo.bar FROM sys.columns

What datatype is foo? It has no meaning of course, but it caught me out once.

梨涡 2024-09-11 22:53:17

从概念上讲,NULL 意味着“缺失的未知值”,它的处理方式与其他值有些不同。

在 MySQL 中,0 或 NULL 表示 false,其他任何值表示 true。布尔运算的默认真值是 1。GROUP

BY 中两个 NULL 值被视为相等。

执行 ORDER BY 时,如果执行 ORDER BY ... ASC,则首先显示 NULL 值;如果执行 ORDER BY ... DESC,则最后显示 NULL 值。

Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values.

In MySQL, 0 or NULL means false and anything else means true. The default truth value from a boolean operation is 1.

Two NULL values are regarded as equal in a GROUP BY.

When doing an ORDER BY, NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC.

-黛色若梦 2024-09-11 22:53:17

NULL 值本质上意味着缺失值,有些人还会使用术语“unknow

” Null 值也不等于任何值,甚至不等于另一个 NULL

看一下这些示例

will print is equal

IF 1 = 1
PRINT 'is equaL'
ELSE
PRINT 'NOT equal'

will print is equal (隐式转换发生)

IF 1 = '1'
PRINT 'is equaL'
ELSE
PRINT 'NOT equal'

将打印不等于

IF NULL = NULL
PRINT 'is equaL'
ELSE
PRINT 'NOT equal'

The value of NULL means in essence a missing value, some people will also use the term unknow

A Null value is also not equal to anything not even another NULL

Take a look at these examples

will print is equal

IF 1 = 1
PRINT 'is equaL'
ELSE
PRINT 'NOT equal'

will print is equal (an implicit conversion happens)

IF 1 = '1'
PRINT 'is equaL'
ELSE
PRINT 'NOT equal'

will print is not equal

IF NULL = NULL
PRINT 'is equaL'
ELSE
PRINT 'NOT equal'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文