小结

1、

mysql> INSERT INTO my_table (phone) VALUES (NULL); 有手机号但是不知道

mysql> INSERT INTO my_table (phone) VALUES ('');没有手机号

http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to

SELECT 1=1,1=NULL,0=NULL,NULL=NULL;

1 NULL NULL NULL

SELECT 1<=>1,1<=>NULL,0<=>NULL,NULL<=>NULL;

1 0 0 1

 

<=>

NULL-safe equal. This operator performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0rather than NULL if one operand is NULL.

The <=> operator is equivalent to the standard SQL IS NOT DISTINCT FROM operator.

For row comparisons, (a, b) <=> (x, y) is equivalent to:

(a <=> x) AND (b <=> y)

DEFAULT  NULL

[SQL]UPDATE tab SET toutiaoid=NULL WHERE NOT toutiaoid>0;
受影响的行: 8
时间: 0.046s

[SQL]

ALTER TABLE tab ADD UNIQUE KEY (toutiaoid);

`toutiaoid` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '头条id',

默认为NULL的字段 可以加unique约束

https://dev.mysql.com/doc/refman/5.7/en/create-index.html

MySQL :: MySQL 8.0 Reference Manual :: B.4.4.3 Problems with NULL Values https://dev.mysql.com/doc/refman/8.0/en/problems-with-null.html

MySQL :: MySQL 8.0 Reference Manual :: 3.3.4.6 Working with NULL Values https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html

3.3.4.6 Working with NULL Values

The NULL value can be surprising until you get used to it. Conceptually, NULL means “a missing unknown value”and it is treated somewhat differently from other values.

To test for NULL, use the IS NULL and IS NOT NULL operators, as shown here:

mysql> SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
| 0 | 1 |
+-----------+---------------+

You cannot use arithmetic comparison operators such as =<, or <> to test for NULL. To demonstrate this for yourself, try the following query:

mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
| NULL | NULL | NULL | NULL |
+----------+-----------+----------+----------+

Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.

In MySQL, 0 or NULL means false and anything else means true. The default truth value from a boolean operation is 1.

This special treatment of NULL is why, in the previous section, it was necessary to determine which animals are no longer alive using death IS NOT NULL instead of death <> NULL.

Two NULL values are regarded as equal in a GROUP BY.

When doing an ORDER BYNULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC.

A common error when working with NULL is to assume that it is not possible to insert a zero or an empty string into a column defined as NOT NULL, but this is not the case. These are in fact values, whereas NULL means “not having a value.” You can test this easily enough by using IS [NOT] NULL as shown:

mysql> SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;
+-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
+-----------+---------------+------------+----------------+
| 0 | 1 | 0 | 1 |
+-----------+---------------+------------+----------------+

Thus it is entirely possible to insert a zero or empty string into a NOT NULL column, as these are in fact NOT NULL. See Section B.4.4.3, “Problems with NULL Values”.

B.4.4.3 Problems with NULL Values

The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULLis the same thing as an empty string ''. This is not the case. For example, the following statements are completely different:

mysql> INSERT INTO my_table (phone) VALUES (NULL);
mysql> INSERT INTO my_table (phone) VALUES ('');

Both statements insert a value into the phone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be regarded as “phone number is not known” and the meaning of the second can be regarded as “the person is known to have no phone, and thus no phone number.”

To help with NULL handling, you can use the IS NULL and IS NOT NULL operators and the IFNULL() function.

In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression. All columns in the following example return NULL:

mysql> SELECT NULL, 1+NULL, CONCAT('Invisible',NULL);

To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression:

mysql> SELECT * FROM my_table WHERE phone = NULL;

To look for NULL values, you must use the IS NULL test. The following statements show how to find the NULLphone number and the empty phone number:

mysql> SELECT * FROM my_table WHERE phone IS NULL;
mysql> SELECT * FROM my_table WHERE phone = '';

See Section 3.3.4.6, “Working with NULL Values”, for additional information and examples.

You can add an index on a column that can have NULL values if you are using the MyISAMInnoDB, or MEMORYstorage engine. Otherwise, you must declare an indexed column NOT NULL, and you cannot insert NULL into the column.

When reading data with LOAD DATA, empty or missing columns are updated with ''. To load a NULL value into a column, use \N in the data file. The literal word NULL may also be used under some circumstances. See Section 13.2.7, “LOAD DATA Statement”.

When using DISTINCTGROUP BY, or ORDER BY, all NULL values are regarded as equal.

When using ORDER BYNULL values are presented first, or last if you specify DESC to sort in descending order.

Aggregate (summary) functions such as COUNT()MIN(), and SUM() ignore NULL values. The exception to this isCOUNT(*), which counts rows and not individual column values. For example, the following statement produces two counts. The first is a count of the number of rows in the table, and the second is a count of the number of non-NULL values in the age column:

mysql> SELECT COUNT(*), COUNT(age) FROM person;

For some data types, MySQL handles NULL values specially. If you insert NULL into a TIMESTAMP column, the current date and time is inserted. If you insert NULL into an integer or floating-point column that has the AUTO_INCREMENT attribute, the next number in the sequence is inserted.

Target Server Type : MYSQL
Target Server Version : 80016
File Encoding : 65001

Date: 2020-03-16 08:28:00
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sum` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3334 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('23', '1');
INSERT INTO `test` VALUES ('333', null);

[SQL]update test set sum=sum+1 where id=333;
受影响的行: 0
时间: 0.012s

[SQL]
update test set sum=sum+1 where id=23;
受影响的行: 1
时间: 0.012s

对于null值增加失败。

NULL-safe equal null 索引 空字符串的更多相关文章

  1. ASP.NET MVC 下UpdateModel可空未填写的参数为Null,为何不是空字符串

    查了好久,终于收到原因: if (bindingContext.ModelMetadata.ConvertEmptyStringToNull && Object.Equals(valu ...

  2. Java空字符串与null的区别和判断字符串是否为空的方法

    Java空字符串与null的区别: 1.类型null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ;""表示的是一个空字符串,也 ...

  3. Java进阶(二十一)java 空字符串与null区别

    java 空字符串与null区别 1.类型 null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ; ""表示的是一个空字符串, ...

  4. Django与SQL语言中——NULL与空字符串的区别

    SQL有指定空值的独特方式,它把空值叫做NULL. Null在数据库中表示 不知道的数据,主要有3种意思: 1)知道数据存在,但不知道具体值. 2)不知道数据是否存在. 3)数据不存在. 在SQL中, ...

  5. 空字符串(“”)和null和空格字符串(" ")的区别

    1.类型 null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ;""表示的是一个空字符串,也就是说它的长度为0,但它是一个字符 ...

  6. Oracle中Null与空字符串' '的区别

    含义解释: 问:什么是NULL? 答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,我们称它为空,ORACLE中,含有空值的表列长度为零. ORACLE允许任何一种数据类型的字段为空,除 ...

  7. MySQL中NULL与空字符串

    一些刚刚接触MySQL的孩子,经常会错误的认为NULL与空字符串’  ’是相同的.这看似是一件不重要的事情,但是在MySQL中,这两者是完全不同的.NULL是指没有值,而”则表示值是存在的,只不过是个 ...

  8. Oracle中的null与空字符串''的区别

    含义解释:问:什么是NULL?答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,我们称它为空,ORACLE中,含有空值的表列长度为零.ORACLE允许任何一种数据类型的字段为空,除了以下 ...

  9. 指针数组 null与空字符串

    指针数组常适用于指向若干字符串,这样使字符串处理更加灵活方便. 在c++中,null表示:对象为空,它是对指针而言的.而""表示:值为空,它是对字符串而言的.

随机推荐

  1. Multipass使用教程

    一.Multipass介绍 Multipass是一种简单的虚拟机工具.它不仅使启用虚拟机变得快速简易,还使管理那些虚拟机变得异常简单,因此可以立即开始针对云.边缘.物联网或任何一种类型的技术进行开发. ...

  2. x264编码demo定制修改介绍

    x264编码器,提供了两个demo来验证编码功能:一个是大而全的x264.c,另外一个是简洁版的example.c. 其中,前者demo,可以配置很多编码参数,但太冗长繁杂,对初学者不太友好.  后者 ...

  3. CentOS7 实战部署tomcat网站服务器

    简介:实战演练tomcat网站服务器的搭建 Tomcat:是一个开源免费的Web应用服务器,性能稳定,是目前比较流行的Web应用服务器   tomcat官网下载: https://tomcat.apa ...

  4. Long类型数据传递到前端数据精度丢失问题

    在开发页面的时候,遇到Long类型的数据,传送给前端遇到精度丢失的问题, 后端发的数据是这个. 前端接收到的数据是这样 解决的途径有二种:1 .在后端把Long类型的数据改成String类型(不推荐) ...

  5. 【代码周边】-GitHub笔记

    ------------恢复内容开始------------ 程序员的宝库github是个好东西,其中开源的项目足够我们的使用,但是如何去精准的获取我们的项目是很多初学者的问题.特别是英语不好的我,一 ...

  6. [LeetCode]42. Trapping Rain Water雨水填坑

    这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水 双指针可以解决 /* 两边指针保证,保证另外一边肯定有能挡住水的地方. 如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只 ...

  7. 事件驱动之JDK观察者模式

    JDK中关于观察者模式主要了解俩个概念 Observer观察者 Observable事件源:当事件源发生某事件时,有两个事情需要注意 1.里面有一个isChange属性  当为false时不会发通知给 ...

  8. Docker之1---介绍和安装

    Docker介绍 Docker是一个开源项目,让应用程序布署在软件货柜下的工作可以自动化进行,借此在Linux操作系统上,提供一个额外的软件抽象层,以及操作系统层虚拟化的自动管理机制. Docker利 ...

  9. Linux 内核参数 优化

    Linux 内核参数 优化 目录 Linux 内核参数 优化 1.编辑内核配置文件 2.参数及简单说明 3.客户端的典型状态转移参数 4.TCP重传参数 5.实现Nginx高并发的内核参数优化 生效配 ...

  10. ShareSdk自己写的Demo

    安卓原生Demo 根据mob发布的ShareSdk编写的demo,只添加了自己使用到的一些平台. 签名和第三方平台的账号都是用的shareSdk官方Demo的. 因为只是为了展示shareSdk用法, ...