当前位置:网站首页>Is not null and in SQL= Difference between null
Is not null and in SQL= Difference between null
2022-06-27 23:36:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
We often meet these two writing methods :IS NOT NULL And !=NULL. Also often meet the database has qualified !=NULL The data of , But the return is an empty collection . actually , It's because we don't understand the difference between them .
By default , Recommended IS NOT NULL Make conditional judgments , because SQL By default WHERE XX!= Null Your judgment will always return to 0 That's ok , But it won't prompt syntax errors .
Why is that ?
SQL Server In the document for Null The comparison of values defines two rules , If in SQL Server 2000 in :
Rule one yes yes ANSISQL(SQL-92) Stipulated Null The comparison results of the values are False, Both Null=Null The value is also False. The other is not allowed to follow ANSISQL standard , namely Null=Null by True.:
For example, data sheet test structure :
Copy code The code is as follows :
ROWNUM DATA
——————-
1 ‘Liu Yang’
2 Null
3 ‘12345’
according to ANSI SQL standard , Neither of the following two queries returns any rows :
Query one : SELECT * FROM test WHERE data=NULL Query two : SELECT * FROM test WHERE data<>NULL
Instead of ANSI SQL standard , Inquire about 1 The second line... Will be returned , Inquire about 2 return 1、3 That's ok .
This is because in the SQL in ,NULL Is a unique data type , It is equivalent to having no value 、 It's an unknown number .NULL And 0、 An empty string 、 The spaces are different . ANSI SQL Standard Null The row of values needs to use the following query :
Copy code The code is as follows :
SELECT * FROM test WHERE data IS NULL
This shows that it is not ANSI SQL In the standard data=NULL Equate to data IS NULL,data<>NULL Equate to data IS NOT NULL.
So we should keep in mind : By default, the keyword is used for comparison “is null” and “is not null”.
If you must use != null To make conditional judgments , You need to add this command statement :SET ANSI_NULLS OFF, At this time, the database enters ANSI SQL Non standard mode , You'll find that IS NOT NULL and != null Is equivalent .
The mode switching command is used here SET ANSI_NULLS[ON/OFF].ON Value adoption ANSI SQL Strict standards ,OFF Value is in non-standard compatibility mode . in addition SET ANSI_DEFAULTS [ON/OFF] Command can also realize standard switching , Only this command controls a group that conforms to SQL-92 Standard settings , Among them is Null Value criteria .
By default , Database management program (DB-Library) yes SET ANSI_NULLS by OFF Of . But most of our applications , It's all through ODBC perhaps OLEDB To access the database , As an open and compatible database access program , Maybe it's a compatibility consideration ,SETANSI_NULLS Value is set to ON. Some problems caused by this should be paid attention to . Applications such as stored procedures or custom functions are based on DB-Library Of , By default ,SETANSI_NULLS by OFF, And in such a program , Out of commission SETANSI_NULLS Modify rules in an environment , Only database configuration parameters can be modified .
For example, in the following case : Your application uses ADODB To access the database , use OleDb perhaps ODBC Data provider . For query one : SELECT * FROM test WHERE data=NULL We can directly send commands to get the query result set , You can also put it in a stored procedure . But the results of the two queries are different . If you use the query command directly , Do not return any rows ; And if you access a stored procedure , Back to page 2 Row data .
Last , We declare once again : By default, the database , do SQL Use keywords when comparing conditional queries “is null” and “is not null”.
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133155.html Link to the original text :https://javaforall.cn
边栏推荐
- Feign implements path escape through custom annotations
- webService
- MySQL十八:写语句的执行过程
- Halcon's region: features of multiple regions (6)
- 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
- seata
- vivado 如何添加时序约束
- 华为伙伴暨开发者大会2022 | 麒麟软件携手华为共建计算产业,共创数智未来
- The National University of Singapore 𞓜 uses model free reinforcement learning to evaluate the energy efficiency of the energy efficiency data center
- 【PCL自学:Segmentation4】基于Min-Cut点云分割
猜你喜欢
【PCL自学:Segmentation3】基于PCL的点云分割:区域增长分割
MySQL十八:写语句的执行过程
【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
未能加载文件或程序集“CefSharp.Core.Runtime.dll”或它的某一个依赖项。 不是有效的 Win32 应用程序。 (异常来自 HRESULT:0x800700C1)
实践torch.fx:基于Pytorch的模型优化量化神器
【微服务|Sentinel】sentinel数据持久化
【IDEA】IDEA 格式化 代码技巧 idea 格式化 会加 <p> 标签
Summary of solutions to cross system data consistency problems
图的存储结构
Usage of vivado vio IP
随机推荐
撰写外文时怎样引用中文文献?
This kind of people began to be robbed by VC with a monthly salary of 80000 yuan
图的存储结构
虽然TCGA数据库有33种癌症
新加坡国立大学|采用无模型强化学习方法评估能源效益数据中心的节能情况
[try to hack] kill evaluation
思源笔记订阅停止直接删云端数据嘛?
ICML 2022: UFRGS |作为最优策略转移基础的乐观线性支持和后继特征
【Vim】使用教程,常用命令,高效使用Vim编辑器
The choice and trade-off between vector recall and literal recall
沉寂了一段时间 ,我又出来啦~
在线JSON转PlainText工具
fiddler 监听不到接口怎么办
小芯片chiplet技术杂谈
Discuz淘宝客网站模板/迪恩淘宝客购物风格商业版模板
C language character pointer and string initialization
跨系统数据一致性问题解决方案汇总
The file or assembly 'cefsharp.core.runtime.dll' or one of its dependencies could not be loaded. Is not a valid Win32 Application. (exception from hresult:0x800700c1)
【PCL自学:Segmentation3】基于PCL的点云分割:区域增长分割
EasyCVR平台路由日志功能的技术实现过程【附代码】