惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - shawnliu

log4net使用guidline(写的很详细) Log4net 简明手册(来自yuhen,写的非常好) 浅谈大型网站动态应用系统架构 使用cfengine来实现服务器的自动化配置 - shawnliu - 博客园 CodeDOM & Emit & MSIL 如何在.NET中实现脚本引擎 (CodeDom篇) 什么是Landing Page? sql server apply与 join等的区别 老是在select中丢掉了NOLOCK有啥后果???deadlock ooo scope_identity(), @@IDENTITY, IDENT_CURRENT()区别 Introduction to SQL SQL Server Indexes [A good reference] 大众点评的年会视频 很搞 十二种标题编写方法,让你流量暴涨[zz] (测试小程序)使用XmlSerializer来连接xml config文件和类 正则表达式 1个月100万封邮件营销实战及总结[zz] B2C Opinions 年薪12万的乞丐给我上了震撼的一课 Eliminate the Use of Temporary Tables For HUGE Performance Gains
SQL Server中奇妙的NULL
shawnliu · 2010-06-10 · via 博客园 - shawnliu

 http://www.cnblogs.com/changbluesky/archive/2010/05/07/1729519.html

ps.懒得写,懒得总结,copy一个总结不错的,不过还有一些没说到,总之,翻过几次错遇到null都要多考虑多测试

相信大家在写SQL时都会有遇到NULL的经历吧,在一个table插入NULL,与NULL作比较等等.

1.NULL意思为缺失的值(missing value).

2.三值逻辑(three-valued-logic: TRUE,FALSE,UNKNOWN). 在SQL中有三个逻辑谓词:TURE,FALSE,UNKNOWN.在大多数的编程语言中只有TRUE和FALSE,而在SQL中独有UNKNOWN,之所有存在与NULL有关.

  比如做如下比较: NULL>32;NULL=NULL;X+NULL>Y;NULL<>NULL.其计算结果均为UNKNOWN.

  可能会有些迷惑,于二值逻辑不同(NOT TURE=FALUSE;NOT FALSE=TRUE)的是NOT UNKNOWN=UNKNOWN.

3.UNKNOWN作为FALSE时的处理. 在SQL中查询过滤时(ON,WHERE,HAVING)会把UNKNOWN作为FALSE处理,这样就不会把计算值为UNKNOWN的行添加到下一个结果集中.

4.UNKNOWN作为TRUE时的处理. 在CHECK约束中UNKNOWN却作为TRUE来处理.

  比如在一个table中添加约束条件,约束年龄必须大于零:alter table test1 add constraint ck_age check (age>0),在插入数据时仍然可以插入NULL值(前提是这列没有定义NOT NULL约束).insert into test1(age) values(NULL)

代码

create table test
(
Name varchar(10),
age int
)--add check constraint age>0
alter table test add constraint ck_age check (age>0)insert into test values
('bluesky',null)select * from test

可以插入NULL值.

5.再谈NULL与NULL的比较,上面已经讲过(NULL=NULL;NULL<>NULL),即NULL与NULL的比较均为UNKNOWN. 但是对于UNIOUE约束,集合操作(如UNION,EXCEPT),排序,分组时,NULL与NULL为认为是等值的.

  5.1 如果一列有UNIQUE约束,就不能插入两个NULL值.

--add unique
alter table test add unique (Name asc)insert into test values
(NULL,12),
(NULL,13)

先定义UNIQUE 约束,insert两个NULL时会出现如下的提示,说明NULL在unique中被当做等值处理.

  5.2 GROUP BY会把NULL分到一个组.

代码

--drop the unique constraint
ALTER TABLE [dbo].[test] DROP CONSTRAINT [UQ__test__737584F63A81B327]--insert two null values
insert into test values
(NULL,12),
(NULL,13)select * from testselect Name,SUM(age)as sumage from test group by Name

 可以看到NULL被分成了一组:

 

  5.2 ORDER BY会把NULL排序在一起.

  5.3 在集合操作时会把NULL当成等值的来处理.

总之: 识别在不同的情况下:NULL操作时被作为UNKNOWN还是作为TRUE,FALSE,有助于更好的写SQL及DB设计.