色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

MySQL null的一些易錯點

瀏覽:4日期:2023-10-07 15:19:23

依據null-values,MySQL的值為null的意思只是代表沒有數據,null值和某種類型的零值是兩碼事,比如int類型的零值為0,字符串的零值為””,但是它們依然是有數據的,不是null.

我們在保存數據的時候,習慣性的把暫時沒有的數據記為null,表示當前我們無法提供有效的信息.

不過使用null但是時候,需要我們注意一些問題.對此MySQL文檔說明如下: problems-with-null

使用null的易錯點

下面我摘取MySQL官方給出的null的易錯點做講解.

對MySQL不熟悉的人很容易搞混null和零值

The concept of the NULL value is a common source of confusion for newcomers to SQL

比如下面這2句SQL產生的數據是獨立的

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

第一句SQL只是表示暫時不知道電話號碼是多少,第二句是電話號碼知道并且記錄為’’

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.”

對null的邏輯判斷要單獨處理

對于是否為null的判斷必須使用專門的語法IS NULL,IS NOT NULL,IFNULL().

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

如果你使用=判斷,那么永遠是false

In SQL, the NULL value is never true in comparison to any other value, even 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

比如你這樣寫,where后判斷的結果永不會是true:

SELECT * FROM my_table WHERE phone = NULL;

如果你使用null和其他數據做計算,那么結果永遠是null,除非MySQL文檔對某些操作做了額外的特殊說明

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

例如:

mysql> SELECT NULL, 1+NULL, CONCAT(’Invisible’,NULL);+------+--------+--------------------------+| NULL | 1+NULL | CONCAT(’Invisible’,NULL) |+------+--------+--------------------------+| NULL | NULL | NULL |+------+--------+--------------------------+1 row in set (0.00 sec)

所以你要對null做邏輯判斷,還是乖乖的使用IS NULL

To look for NULL values, you must use the IS NULL test對有null值的列做索引要額外預料到隱藏的細節

只有InnoDB,MyISAM,MEMORY 存儲引擎支持給帶有null值的列做索引

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

索引的長度會比普通索引大1,也就是略微耗內存點

Due to the key storage format, the key length is one greater for a column that can be NULL than for a NOT NULL column.

對null值做分組,去重,排序會被特殊對待

和上文講的=null永遠是false相反,這時null 被認為是相等的.

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

對null排序會被特殊對待

null值要么被排在最前面,要么最后面

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

聚合操作時null被忽略

Aggregate (group) functions such as COUNT(), MIN(), and SUM() ignore NULL values

例如count(*)不會統計值為null的數據.

The exception to this is COUNT(*), 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;

以上就是MySQL null的一些易錯點的詳細內容,更多關于MySQL null的資料請關注好吧啦網其它相關文章!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: av中文字幕网免费观看 | 亚洲精品高清国产一线久久97 | 国产亚洲精品久久久久久 | 国产一级一片 | 久草久草久草 | 久久国产精品久久国产精品 | 盗摄偷拍a在线观看 | 亚洲天堂中文网 | 97免费公开视频 | 无国产精品白浆是免费 | 日韩精品一区在线观看 | 精品国产品欧美日产在线 | 欧美精品日日鲁夜夜添 | 先锋影音xfyy5566男人资源 | 最新色网址 | 免费一级a毛片免费观看欧美大片 | 成人免费观看网欧美片 | 久草在线视频在线观看 | 夜夜骚视频 | 久久精品在现线观看免费15 | 国产成a人片在线观看视频 国产成版人视频网站免费下 | 性久久久久久久 | 国产三级精品久久三级国专区 | 日本三级视频在线 | 午夜限制r级噜噜片一区二区 | 99久久精品免费 | 精品视频在线一区 | 久久不见久久见免费影院www日本 | 尤物蜜芽福利国产污在线观看 | 欧美日韩精品一区二区三区不卡 | 交videos人妖 | 免费看特级淫片日本 | 欧美一级成人毛片影院 | 成人免费视频网 | 国产裸体美女视频全黄 | 欧美日本免费观看αv片 | 国产精品一一在线观看 | 亚洲bbbbbxxxxx精品三十七 | 一区二区三区在线免费视频 | 久久一本色系列综合色 | 黄色免费在线网址 |