濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > MySQL null的一些易錯(cuò)點(diǎn)

MySQL null的一些易錯(cuò)點(diǎn)

熱門(mén)標(biāo)簽:常州電銷外呼系統(tǒng)一般多少錢(qián) 沃克斯電梯外呼線路圖 房產(chǎn)智能外呼系統(tǒng)品牌 天智外呼系統(tǒng) 云南語(yǔ)音外呼系統(tǒng)平臺(tái) 福州呼叫中心外呼系統(tǒng)哪家好 北京人工外呼系統(tǒng)價(jià)錢(qián) 地圖標(biāo)注被騙三百怎么辦 400電話鄭州申請(qǐng)

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

我們?cè)诒4鏀?shù)據(jù)的時(shí)候,習(xí)慣性的把暫時(shí)沒(méi)有的數(shù)據(jù)記為null,表示當(dāng)前我們無(wú)法提供有效的信息.

不過(guò)使用null但是時(shí)候,需要我們注意一些問(wèn)題.對(duì)此MySQL文檔說(shuō)明如下: problems-with-null

使用null的易錯(cuò)點(diǎn)

下面我摘取MySQL官方給出的null的易錯(cuò)點(diǎn)做講解.

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

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

比如下面這2句SQL產(chǎn)生的數(shù)據(jù)是獨(dú)立的

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

第一句SQL只是表示暫時(shí)不知道電話號(hào)碼是多少,第二句是電話號(hào)碼知道并且記錄為''

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

對(duì)null的邏輯判斷要單獨(dú)處理

對(duì)于是否為null的判斷必須使用專門(mén)的語(yǔ)法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.

如果你使用=判斷,那么永遠(yuǎn)是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

比如你這樣寫(xiě),where后判斷的結(jié)果永不會(huì)是true:

SELECT * FROM my_table WHERE phone = NULL;

如果你使用null和其他數(shù)據(jù)做計(jì)算,那么結(jié)果永遠(yuǎn)是null,除非MySQL文檔對(duì)某些操作做了額外的特殊說(shuō)明

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)

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

To look for NULL values, you must use the IS NULL test

對(duì)有null值的列做索引要額外預(yù)料到隱藏的細(xì)節(jié)

只有InnoDB,MyISAM,MEMORY 存儲(chǔ)引擎支持給帶有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.

索引的長(zhǎng)度會(huì)比普通索引大1,也就是略微耗內(nèi)存點(diǎn)

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.

對(duì)null值做分組,去重,排序會(huì)被特殊對(duì)待

和上文講的=null永遠(yuǎn)是false相反,這時(shí)null 被認(rèn)為是相等的.

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

對(duì)null排序會(huì)被特殊對(duì)待

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

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

聚合操作時(shí)null被忽略

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

例如count(*)不會(huì)統(tǒng)計(jì)值為null的數(shù)據(jù).

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的一些易錯(cuò)點(diǎn)的詳細(xì)內(nèi)容,更多關(guān)于MySQL null的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 為什么mysql字段要使用NOT NULL
  • MySQL中關(guān)于null值的一個(gè)小問(wèn)題
  • MySQL null與not null和null與空值''''''''的區(qū)別詳解
  • MySQL IFNULL判空問(wèn)題解決方案
  • 區(qū)分MySQL中的空值(null)和空字符('''')
  • 詳解mysql不等于null和等于null的寫(xiě)法
  • Mysql NULL導(dǎo)致的神坑
  • mysql中null(IFNULL,COALESCE和NULLIF)相關(guān)知識(shí)點(diǎn)總結(jié)
  • 詳解mysql三值邏輯與NULL

標(biāo)簽:珠海 拉薩 沈陽(yáng) 移動(dòng) 鹽城 徐州 沈陽(yáng) 黔東

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL null的一些易錯(cuò)點(diǎn)》,本文關(guān)鍵詞  MySQL,null,的,一些,易錯(cuò),點(diǎn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL null的一些易錯(cuò)點(diǎn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MySQL null的一些易錯(cuò)點(diǎn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    余庆县| 漳州市| 军事| 满城县| 揭东县| 泽库县| 无极县| 乡宁县| 湖口县| 洪雅县| 旺苍县| 宜城市| 大宁县| 吉首市| 荣成市| 江口县| 东阿县| 石狮市| 竹北市| 繁峙县| 安康市| 巴林右旗| 滦南县| 伊吾县| 禹州市| 淮南市| 黑龙江省| 新巴尔虎右旗| 临朐县| 保山市| 苍山县| 长沙县| 铁岭县| 肃南| 胶州市| 刚察县| 鹰潭市| 道孚县| 浦县| 炉霍县| 登封市|