/*===========================<一>==========================
在SQL中逻辑表达式的值有三种: 1.TRUE 2.FALSE 3.UNKNOWNUNKNOWN逻辑值通常出现在包含NULL的逻辑表达式中,例如: 1. NULL > 0 2. NULL = NULL 3. 1 + NULL = X
============================<二>==========================
1. 在所有的查询筛选器中【ON、WHERE、HAVING】,都把UNKNOWN当 做FALSE处理;2. 在CHECK约束中UNKNOWN的值被当做TRUE处理;3. 在UNIQUE约束、排序操作、分组操作UNKNOWN的值被当做TRUE处理; ============================<三>==========================*/
首先,你的测试数据库中创建测试表,脚本如下:
USE Study --/*我的测试数据库是Study,这里需要修改成你的数据库名。*/GOCREATE TABLE Salary( SalaryID NVARCHAR(36) NOT NULL ,EmployeeID NVARCHAR(36) ,Salary INTCONSTRAINT [PK_Salary] PRIMARY KEY CLUSTERED ( [SalaryID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[Salary] WITH NOCHECK ADD CONSTRAINT [CK_Salary] CHECK (([Salary] > 0))GOALTER TABLE [dbo].[Salary] CHECK CONSTRAINT [CK_Salary]GOALTER TABLE Salary ADD CONSTRAINT AK_Employee UNIQUE (EmployeeID);GOView Code
然后,向测试表中插入数据。
--插入正常数据INSERT INTO [Study].[dbo].[Salary] ([SalaryID] ,[EmployeeID] ,[Salary])VALUES (NEWID() ,NEWID() ,4800)GO--插入EmployeeID为NULL的值INSERT INTO [Study].[dbo].[Salary] ([SalaryID] ,[EmployeeID] ,[Salary])VALUES (NEWID() ,null ,5600)GO--插入两条SALARY为NULL的值INSERT INTO [Study].[dbo].[Salary] ([SalaryID] ,[EmployeeID] ,[Salary])VALUES (NEWID() ,NEWID() ,null)GOINSERT INTO [Study].[dbo].[Salary] ([SalaryID] ,[EmployeeID] ,[Salary])VALUES (NEWID() ,NEWID() ,null)GOView Code
查询一下,插入数据以后的表中的数据情况:
SELECT * FROM [Salary]View Code
表数据:
SalaryID | EmployeeID | Salary |
2D5E8F91-8312-4FA4-BF0C-2643E688555D | NULL | 5600 |
6A22894A-9D7B-4964-9E1B-EF1307D86AFA | 70AD8361-B392-41A3-B30F-6F1B04281808 | NULL |
B6D65637-D65E-4E62-BD15-741BD1F8F8C0 | CAA18E7E-A792-45B8-82CE-80F719359F62 | NULL |
F0FDEA1F-1814-406C-A91C-146751B6BD92 | 2BA81F41-B9BC-4F2A-ABAA-662D1B1ED032 | 4800 |
1. 验证Check约束,建表时我加了Salary的Check约束,Salary > 0;
INSERT INTO [Study].[dbo].[Salary] ([SalaryID] ,[EmployeeID] ,[Salary])VALUES (NEWID() ,NEWID() ,-1000)GOView Code
分析:
我插入-1000时有如下消息提示(说明我的约束是起作用的):
The INSERT statement conflicted with the CHECK constraint "CK_Salary". The conflict occurred in database "Study", table "dbo.Salary", column 'Salary'.
前面我已经插入了两条Salary为NULL的记录,说明在Check约束中NULL > 0的逻辑值(UNKNOWN) 是被视为TRURE值。
2. 在查询筛选器中,UNKNOWN被视作FALSE;
SELECT * FROM SalaryWHERE Salary > 4000View Code
分析:
查询结果为两条数据,Salary分别为5600和4800.而两条Salary为NULL的没有被查询出来。 说明NULL > 4000的逻辑值(UNKNOWN)被视为FALSE.
3. GROUP BY/ORDER BY中UNKNOWN被视作TRURE;
SELECT Salary FROM SalaryGROUP BY SalarySELECT Salary FROM SalaryORDER BY Salary ASCView Code
查询结果分别为:
1.分组查询
Salary |
NULL |
4800 |
5600 |
2.排序查询
Salary |
NULL |
NULL |
4800 |
5600 |
分析: 根据以下查询结果我们可以分析出:NULL == NULL的逻辑值(UNKNOWN),被视作TURE处理。
4. UNIQUE约束中,UNKNOWN被视作FALSE;EmployeeID已经加了UNIQUE约束,前面我已经
插入了一条EmployeeID为NULL的值,下面我再插入一条EmployeeID为NULL的值的记录:
INSERT INTO [Study].[dbo].[Salary] ([SalaryID] ,[EmployeeID] ,[Salary])VALUES (NEWID() ,null ,5600)GOView Code
执行结果,提示如下信息:
Violation of UNIQUE KEY constraint 'AK_Employee'. Cannot insert duplicate key in object 'dbo.Salary'.
分析: 则说明 NULL == NULL的逻辑值(UNKNOWN),被视作FALSE处理。===========================<End>============================
新闻热点
疑难解答