Microsoft SQL Server 2008技术内幕:T-SQL查询---------逻辑查询处理

本文深入探讨了SQL中的三值逻辑,包括TRUE、FALSE和UNKNOWN的处理方式,尤其是在查询筛选器、约束检查及聚合函数中的表现。揭示了NULL在不同SQL元素中的不一致处理,如在UNIQUE约束、SET操作符、排序和分组中的行为,并对比了COUNT函数在NULL值存在与否时的不同结果。

Three-Valued Logic(三值逻辑)

SQL values: TRUE, FALSE, UNKNOWN

由于查询筛选器中 (ON, WHERE, and HAVING) 将UNKNOW认定为FALSE,所以代入后表达式为 (NULL=NULL,NULL>5, NULL<NULL)之类的行集都会被过滤掉而不出现在结果集中.

UNKNOWN logical results and NULLs are treated inconsistently in different elements of
the language:

all query filters (ON, WHERE, and HAVING) treat UNKNOWN 
like FALSE. A row for which a filter is UNKNOWN is eliminated from the result set.

an UNKNOWN value in a CHECK constraint is actually treated like TRUE.
Suppose you have a CHECK constraint in a table to require that the salary column be
greater than zero. A row entered into the table with a NULL salary is accepted because
(NULL > 0) is UNKNOWN and treated like TRUE in the CHECK constraint.

A comparison between two NULLs in a filter yields UNKNOWN, which, as I mentioned
earlier, is treated like FALSE—as if one NULL is different than another.

for UNIQUE constraints, set operators (such as UNION and EXCEPT), 
and sorting or grouping operations, NULLs are treated as equal:

          ■   You cannot insert into a table two rows with a NULL in a column that has a

              UNIQUE constraint defi ned on it. T-SQL violates the standard on this point.

          ■   A GROUP BY clause groups all NULLs into one group.

          ■   An ORDER BY clause sorts all NULLs together.

          ■   Set operators treat NULLs as equal when comparing rows from the two sets.

COUNT(NULL) 返回值为0

COUNT(*) 与 COUNT(COLUMN_NAME)区别就在于有无NULL值

SELECT  语句中的别名只能在order by阶段 中引用.由于SQL 语句是同时执行的,所以没法用.

SELECT TOP N WITH TIES... ORDER BY ...

Step 5-3: Apply the TOP Option
  The TOP option is a feature specifi  c to T-SQL that allows you to specify a number or percentage
of rows (rounded up) to return. The specifi  ed number of rows is selected based on the query’s
ORDER BY clause. Traditionally, and according to the ANSI SQL standard, ORDER BY is supposed
to serve a presentation purpose. However, when the TOP option is specifi  ed, the ORDER BY
clause also serves a logical purpose— answering the question “top according to what order?”
Table VT5-3 is generated.
  As mentioned, this step relies on the query’s ORDER BY clause to determine which rows are
  considered the “fi  rst” requested number of rows. If an ORDER BY clause with a unique ORDER
BY list is specifi  ed in a query, the result is deterministic. That is, only one correct result is possible,
containing the fi  rst requested number of rows based on the specifi  ed order. Similarly, when an
ORDER BY clause is specifi  ed with a non-unique ORDER BY list but the TOP option is specifi  ed
WITH TIES, the result is also deterministic. SQL Server inspects the last row that was returned
and returns all other rows from the table that have the same sort values as the last row.
  However, when a non-unique ORDER BY list is specifi  ed without the WITH TIES option, or
ORDER BY is not specifi  ed at all, a TOP query is nondeterministic. That is, the rows returned
are the ones that SQL Server happened to access fi  rst, and there might be different results
that are considered correct. If you want to guarantee determinism, a TOP query must have
either a unique ORDER BY list or the WITH TIES option. 

OVER 子句出现在select 阶段或order by 阶段,因此像下面的查询

select custid, count(*)over(partiton by custid) as numbers from orders where shipcountry="china"

统计出的数值是先除去china的行,然后再进行的行数统计

 

转载于:https://www.cnblogs.com/Gravin-Gu/archive/2013/01/28/2880717.html

注:分二卷,点击上传者查看第二卷地址。亲,本人纯手工添加了书签哦!!方便阅读  《Microsoft SQL Server 2008技术内幕:T-SQL语言基础》是Microsoft SQL Server 2008系列中的一本。书中全面深入地介绍了T-SQL的基本元素,以及SQL Server 2008中新增加的一些特性。主要包括SQL的基础理论、逻辑查询处理、SELECT查询、连接和子查询、表表达式、过滤和分组、透视转换、修改数据、事务和一致性的处理、可编程对象等内容。   书中并非系统地罗列T-SQL的各种语法元素,而是结合实践中的各种问题,教读者如何用SQL作为语言工具来思考问题,揭示基于集合查询的强大威力。本书内容丰富、文字简洁明快,列举的实例具有一定的难度,而且实用性较强,可以把它们作为解决实际问题的标准模板。阅读本书,可以充分地理解T-SQL语言和获得良好的编程实践,学会如何编写更加有效而强大的查询。书中大部分章节后面都提供了练习题目,可以帮助读者更好地掌握所学的内容。   《Microsoft SQL Server 2008技术内幕:T-SQL语言基础》适合须要学习T-SQL的各级程序员和数据库专业人员,是他们快速掌握T-SQL的必备参考图书。 致谢 引言 第1章 T-SQL查询和编程基础 1.1 理论背景 1.2 SQL SERVER体系结构 1.3 创建表和定义数据完整性 1.4 总结 第2章 单表查询 2.1 SELECT语句的元素 2.2 谓词和运算符 2.3 CASE表达式 2.4 NULL值 2.5 同时操作(ALL-AT-ONCE OPERATION) 2.6 处理字符数据 2.7 处理日期和时间数据 2.8 查询元数据 2.9 总结 2.10 练习 2.11 解决方案 第3章 联接查询 3.1 交叉联接 3.2 内联接 3.3 特殊的联接实例 3.4 外联接 3.5 总结 3.6 练习 3.7 解决方案 第4章 子查询 4.1 独立子查询 4.2 相关子查询 4.3 高级子查询 4.4 总结 4.5 练习 4.6 解决方案 第5章 表表达式 5.1 派生表 5.2 公用表表达式(CTE) 5.3 视图 5.4 内联表值函数 5.5 APPLY运算符 5.6 总结 5.7 练习 5.8 解决方案 第6章 集合运算 6.1 UNION(并集)集合运算 6.2 INTERSECT(交集)集合运算 6.3 EXCEPT(差集)集合运算 6.4 集合运算的优先级 6.5 避开不支持的逻辑查询处理 6.6 总结 6.7 练习 6.8 解决方案 第7章 透视、逆透视及分组集 7.1 透视转换 7.2 逆透视转换 7.3 分组集 7.4 总结 7.5 练习 7.6 解决方案 第8章 数据修改 8.1 插入数据 8.2 删除数据 8.3 更新数据 8.4 合并数据 8.5 通过表表达式修改数据 8.6 带有TOP选项的数据更新 8.7 OUTPUT子句 8.8 总结 8.9 练习 8.10 解决方案 第9章 事务和并发 9.1 事务 9.2 锁定和阻塞 9.3 隔离级别 9.4 死锁 9.5 总结 9.6 练习 第10章 可编程对象 10.1 变量 10.2 批处理 10.3 流程控制元素 10.4 游标 10.5 临时表 10.6 动态SQL 10.7 例程 10.8 错误处理 10.9 总结 附录A SQL SERVER使用入门 索引
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值