SQL Server 中的 NULL 值是什么?
我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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
returnsUNKNOWN
.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.
我相信对于允许空值的每一列,行都有一个空位图。如果指定列中的行为空,则位图中的位为 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.
SQL Server 行格式在 MSDN 中进行了描述,并在各种博客上进行了分析,例如 Paul Randal 的 记录剖析。重要的信息是记录结构:
因此 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:
So NULL fields have a bit set in the NULL bitmap.
我怀疑面试官想让你确切地知道 SQL Server 如何存储
null
,这样的问题的重点是让你思考如何存储特殊值。您不能使用哨兵值(或幻数),因为这会使包含该值的任何行突然变为null
。有很多方法可以实现这一目标。我想到的最简单的 2 种方法是为每个可空值存储一个标志,该标志基本上是一个
isNull
标志(这也是Nullable
的工作原理) 。网)。第二种方法是为每一行存储一个空标志位图,每一列一个。当面对这样的面试问题时,最糟糕的反应就是坐着发呆。大声思考,承认您不知道 SQL Server 是如何做到这一点的,然后提出一些听起来合理的方法。您还应该准备好谈论为什么选择一种方法而不是另一种方法,以及每种方法的优点和缺点。
I doubt the interviewers wanted you to know exactly how SQL server stores
null
s, 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 becomenull
.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 howNullable<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.
正如比尔所说,这是一种状态而不是价值。
在 SQL Server 表的一行中,它存储在空位图中:实际上没有存储任何值。
SQL Server 的一个怪癖:
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:
What datatype is foo? It has no meaning of course, but it caught me out once.
从概念上讲,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.
NULL 值本质上意味着缺失值,有些人还会使用术语“unknow
” Null 值也不等于任何值,甚至不等于另一个 NULL
看一下这些示例
will print is equal
will print is 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
will print is equal (an implicit conversion happens)
will print is not equal