Mysql的NULL和Empty String
本文基于Mysql5.7版本的参考资料:
https://dev.mysql.com/doc/refman/5.7/en/working-with-null.html
https://dev.mysql.com/doc/search/?d=12&p=1&q=null+empty+string
https://dev.mysql.com/doc/refman/5.7/en/static-format.html
https://dev.mysql.com/doc/refman/5.7/en/dynamic-format.html
昨天新建一个表用于存储表结构信息:
create table tablist(TABLE_SCHEMA varchar(40),TABLE_NAME varchar(40),COLUMN_NAME varchar(40),COLUMN_TYPE varchar(40),
IS_NULLABLE varchar(10),COLUMN_DEFAULT varchar(40),COLUMN_COMMENT varchar(1000),REMARK varchar(2000)); insert into tablist(TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,COLUMN_COMMENT)
select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,COLUMN_COMMENT
from information_schema.`COLUMNS` where TABLE_SCHEMA='leo';
然后查询tablist表:

看看有哪些列没有comment于是:
select * from tablist where COLUMN_COMMENT is null;
查到的结果居然是Empty set。不过从以上查询结果和navicat都能看出:null值在结果集中显示的是'null'的单词,而空字符串则显示为空。
使用select * from tablist where COLUMN_COMMENT='';查询正常。
在MySQL中NULL值和空字符''是不一样的,这点可能会引起很多新人的困惑,因此查询官网相关的页面总结如下:
- 两者的查询方式不一样:NULL值查询使用is null/is not null查询,而empty string可以使用=或者!=、<、>等算术运算符,这点算是最主要的区别了。
以下列出的几点算不上区别,只是一些NULL和空字符的使用总结:
- 在MySQL当中,0和NULL默认可以作为布尔类型的false,任意其他值可以用于表示True,不过一般用1作为True的默认布尔值。
- 在MySQL当中,NULL值在group by、order by、distinct语句中被认为是相等的,会被分在同一组。
- 对于MySQL的unique index来说,null值是可以存在多个的,也就是说unique index不会过滤null值。
- 在MySQL当中,使用order by对包含NULL值的列排序,NULL值会默认排在前边,order by ... desc时排在最后。
- 同Oracle、MSSQL一样你可以在包含NULL值的列上创建索引,但仅限于MyISAM, InnoDB和MEMORY存储引擎。
- 默认情况下LOAD DATA会把空字符或缺失字段全部设置为空字符。如果想要插入NULL值请使用\N,在某些特殊情况下也可以使用NULL字符,但需要设置正确的LOAD DATA语法项,参考https://dev.mysql.com/doc/refman/5.7/en/load-data.html。
- 在使用COUNT(), MIN(), and SUM()等函数时,NULL值会被忽略,当然count(*)除外。
- 对于TIMESTAMP列,插入的NULL值会被CURRENT_TIMESTAMP替代。
- 对于myisam引擎,null值占用额外的存储空间(1bit),空字符串则完全不占用空间。
- 对于索引查询的影响,无论是NULL还是空字符目前的测试结果表明都没有太大区别,这点与oracle中is null的查询可能会导致全表扫描不一样。
- 以上提到的关于NULL值的分组、聚合等,基本都适用于空字符。
Mysql的NULL和Empty String的更多相关文章
- Mysql 数据库默认值选 ''" 、Null和Empty String的区别
两者的查询方式不一样:NULL值查询使用is null/is not null查询,而empty string可以使用=或者!=.<.>等算术运算符,这点算是最主要的区别了. 对于myis ...
- Summary: Difference between null and empty String
String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() i ...
- 'EF.Utility.CS.ttinclude' returned a null or empty string.
需要安装https://www.microsoft.com/en-us/download/details.aspx?id=40762
- mysql default null empty string concat varchar text
text不可设置默认值 null empty string 前者update 初始值时 我响应,但不报错
- [shiro] Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
访问某页面时,出现了这个异常: java.lang.IllegalArgumentException: Wildcard string cannot be null or empty. Make su ...
- Check if a string is NULL or EMPTY using PowerShell
http://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889 Check if a s ...
- (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
访问某页面时,出现了这个异常: java.lang.IllegalArgumentException: Wildcard string cannot be null or empty. Make su ...
- (转)Java 中关于String的空对象(null) ,空值(empty),空格
原文出处:Java 中关于String的空对象(null) ,空值(empty),空格 定义 空对象: String s = null; 空对象是指定义一个对象s,但是没有给该对象分配空间,即没有实例 ...
- Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...
随机推荐
- Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest
Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePull ...
- Perl列表相关函数
内置的列表函数有: grep, join, map, qw//, reverse, sort, unpack join:将多个元素使用给定字符串联起来join grep:从列表中筛选符合条件的元素执行 ...
- linux四剑客-grep/find/sed/awk/详解-技术流ken
四剑客简介 相信接触过linux的大家应该都学过或者听过四剑客,即sed,grep,find,awk,有人对其望而生畏,有人对其爱不释手.参数太多,变化形式太多,使用超级灵活,让一部分人难以适从继而望 ...
- [Linux] Nginx 提供静态内容和优化积压队列
1.try_files指令可用于检查指定的文件或目录是否存在; NGINX会进行内部重定向,如果没有,则返回指定的状态代码.例如,要检查对应于请求URI的文件是否存在,请使用try_files指令和$ ...
- Word转MD文件
直接把网页的内容复制,丢进下面的网站,转为html格式,复制html代码文本 WordHtml 把代码文本放在这里 Html转MD
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
- 元类实现ORM
1. ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,"Object Relational Mapping",即对象-关系映射,简称ORM. ...
- Android Service解析
Android Service是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件,它分为两种工作状态,一种是启动状态,主要用于执行后台计算:另一种是绑定状态,主要用于其他组件和Service ...
- could not read Username for 'https://github.com': No error
用idea往github上push代码的时候,突然的不能用了. 报could not read Username for 'https://github.com': No error错误. 原因不明. ...
- angular bootstrap timepicker TypeError: Cannot set property '$render' of undefined
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...