国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

mysql的主鍵索引為什么不能null

這篇具有很好參考價(jià)值的文章主要介紹了mysql的主鍵索引為什么不能null。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


這是一個(gè)非常奇怪且有趣的問題??梢酝ㄟ^官方文檔進(jìn)行解讀

https://dev.mysql.com/doc/refman/5.7/en/glossary.html

官方文檔對null的描述

A special value in SQL, indicating the absence of data. Any arithmetic operation or equality test involving a NULL value, in turn produces a NULL result. (Thus it is similar to the IEEE floating-point concept of NaN, “not a number”.) Any aggregate calculation such as AVG() ignores rows with NULL values, when determining how many rows to divide by. The only test that works with NULL values uses the SQL idioms IS NULL or IS NOT NULL.

NULL values play a part in index operations, because for performance a database must minimize the overhead of keeping track of missing data values. Typically, NULL values are not stored in an index, because a query that tests an indexed column using a standard comparison operator could never match a row with a NULL value for that column. For the same reason, unique indexes do not prevent NULL values; those values simply are not represented in the index. Declaring a NOT NULL constraint on a column provides reassurance that there are no rows left out of the index, allowing for better query optimization (accurate counting of rows and estimation of whether to use the index).

Because the primary key must be able to uniquely identify every row in the table, a single-column primary key cannot contain any NULL values, and a multi-column primary key cannot contain any rows with NULL values in all columns.

Although the Oracle database allows a NULL value to be concatenated with a string, InnoDB treats the result of such an operation as NULL.

從這個(gè)里面我們可以得出答案,null暗示著數(shù)據(jù)的缺失,對null的算術(shù)運(yùn)算和相等測試得到的結(jié)果還會(huì)是一個(gè)null。null有點(diǎn)類似IEEE 中浮點(diǎn)數(shù)的 NAN not a number的概念(其實(shí)有很多數(shù)都是NAN)。
從這里我們也可以看出: null == null的結(jié)果為 null 而不是 true 是缺失的,可以認(rèn)為null 不是唯一的,就像 IEEE的浮點(diǎn)數(shù) NAN一樣,不是只有一個(gè)值的是一族值的總稱。

這里還有一點(diǎn)提到了: null通常不被放進(jìn)索引中,這也是為什么索引會(huì)因?yàn)閚ull而失效。

再看官方文檔對 primary key索引的說明

A set of columns—and by implication, the index based on this set of columns—that can uniquely identify every row in a table. As such, it must be a unique index that does not contain any NULL values.

InnoDB requires that every table has such an index (also called the clustered index or cluster index), and organizes the table storage based on the column values of the primary key.

When choosing primary key values, consider using arbitrary values (a synthetic key) rather than relying on values derived from some other source (a natural key).

See Also clustered index, index, natural key, synthetic key.

這里就很清晰了,主鍵索引是為了,唯一確定每一條數(shù)據(jù),null因?yàn)槿笔В恢?,所以不能唯一確定每一條數(shù)據(jù)(因?yàn)樗阈g(shù)運(yùn)算得到的是null而不是一個(gè)確定的值 而是一個(gè)null, 只能進(jìn)行 IS NULL 和 not null進(jìn)行運(yùn)算)。

這里的設(shè)計(jì)其實(shí)也是為了符合規(guī)范SQL1992

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

A unique constraint is satisfied if and only if no two rows in
a table have the same non-null values in the unique columns. In
addition, if the unique constraint was defined with PRIMARY KEY,
then it requires that none of the values in the specified column or
columns be the null value.

說白了也是要符合制定的規(guī)范。

題外話:唯一索引和null

唯一索引可以有null值,且可以有多個(gè)null值, 因?yàn)?null 跟 null進(jìn)行相等的比較的時(shí)候,得到結(jié)果是 null。如果 null 跟 null進(jìn)行相等比較的時(shí)候得到的結(jié)果是 相等的, 那么唯一索引可以擁有一個(gè)null值,而不是多個(gè)null值。文章來源地址http://www.zghlxwxcb.cn/news/detail-634841.html

到了這里,關(guān)于mysql的主鍵索引為什么不能null的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • MySQl有哪些索引(種類)?索引特點(diǎn)?為什么要使用索引?

    普通索引:僅加速查詢 唯一索引:加速查詢 + 列值 唯一(可以有null) 主鍵索引:加速查詢 + 列值 唯一(不可以有null)+ 表中只有一個(gè) 組合索引: 多列值組成一個(gè)索引 ,專門用于組合搜索,其效率大于索引合并 全文索引:對文本的內(nèi)容進(jìn)行分詞,進(jìn)行搜索 索引合并:使用

    2024年02月07日
    瀏覽(31)
  • MySQL為什么選擇B+樹創(chuàng)建索引

    MySQL為什么選擇B+樹創(chuàng)建索引

    將磁盤中存儲(chǔ)的所有數(shù)據(jù)記錄依次加載,與給定條件對比,直到找到目標(biāo)記錄; 類比數(shù)組結(jié)構(gòu)的線性查找,效率較低; 結(jié)合數(shù)組和鏈表結(jié)構(gòu)(或者樹結(jié)構(gòu))存儲(chǔ)數(shù)據(jù); 通過哈希函數(shù)(散列函數(shù))計(jì)算哈希地址,相同輸入在固定函數(shù)下輸出保持不變; 哈希結(jié)構(gòu)會(huì)發(fā)生哈希沖突

    2024年02月13日
    瀏覽(20)
  • MySQL為什么采用B+樹作為索引底層數(shù)據(jù)結(jié)構(gòu)?

    MySQL為什么采用B+樹作為索引底層數(shù)據(jù)結(jié)構(gòu)?

    ????????索引就像一本書的目錄,通過索引可以快速找到我們想要找的內(nèi)容。那么什么樣的數(shù)據(jù)結(jié)構(gòu)可以用來實(shí)現(xiàn)索引呢?我們可能會(huì)想到:二叉查找樹,平衡搜索樹,或者是B樹等等一系列的數(shù)據(jù)結(jié)構(gòu),那么為什么MySQL最終選擇了B+樹作為索引的數(shù)據(jù)結(jié)構(gòu)呢? ? ? ? ? 要想

    2024年02月16日
    瀏覽(25)
  • MySQL為什么要使用B+樹做索引?MySQL索引存儲(chǔ)模型推演,B+樹在MySQL的落地形式

    MySQL為什么要使用B+樹做索引?MySQL索引存儲(chǔ)模型推演,B+樹在MySQL的落地形式

    user_innodb這張表里有4個(gè)字段,id,name,gender,phone。 當(dāng)這張表有500萬條數(shù)據(jù),在沒有索引的name字段上執(zhí)行一條where查詢: 如果name字段上有索引呢?我們在name字段上面創(chuàng)建一個(gè)索引,再來執(zhí)行一下查詢: 我們再來執(zhí)行一下select語句。 我們會(huì)發(fā)現(xiàn),有索引的查詢和沒有索引的

    2024年02月16日
    瀏覽(31)
  • MySQL 索引為什么使用 B+ 樹,而不使用紅黑樹 / B 樹 ?

    MySQL 索引為什么使用 B+ 樹,而不使用紅黑樹 / B 樹 ?

    首先 B 樹和 B+ 樹 都是多叉搜索樹,然后我們先來觀察一下 B+ 樹和 B 樹的數(shù)據(jù)結(jié)構(gòu): B+ 樹的數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn) ?B 樹的數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn) 【B+ 樹相較于 B 樹的優(yōu)勢】 1. IO 次數(shù)更少(查詢效率更高) ????????B+ 樹的非葉子節(jié)點(diǎn)不存放實(shí)際的數(shù)據(jù),僅存放索引,因此數(shù)據(jù)量相同的情況

    2024年02月12日
    瀏覽(25)
  • MSQL系列(十二) Mysql實(shí)戰(zhàn)-為什么索引要建立在被驅(qū)動(dòng)表上

    MSQL系列(十二) Mysql實(shí)戰(zhàn)-為什么索引要建立在被驅(qū)動(dòng)表上

    Mysql實(shí)戰(zhàn)-為什么索引要建立在被驅(qū)動(dòng)表上 前面我們講解了B+Tree的索引結(jié)構(gòu),也詳細(xì)講解下 left Join的底層驅(qū)動(dòng)表 選擇原理,那么今天我們來看看到底如何用以及如何建立索引和索引優(yōu)化 開始之前我們先提一個(gè)問題, 為什么索引要建立在被驅(qū)動(dòng)表上 ? 1.建表及測試數(shù)據(jù) 我們先

    2024年02月08日
    瀏覽(44)
  • MySQL索引為什么選擇B+樹,而不是二叉樹、紅黑樹、B樹?

    MySQL索引為什么選擇B+樹,而不是二叉樹、紅黑樹、B樹?

    二叉樹是一種二分查找樹,有很好的查找性能,相當(dāng)于二分查找。 二叉樹的非葉子節(jié)值大于左邊子節(jié)點(diǎn)、小于右邊子節(jié)點(diǎn)。 原因: 但是當(dāng)N比較大的時(shí)候,樹的深度比較高。數(shù)據(jù)查詢的時(shí)間主要依賴于磁盤IO的次數(shù),二叉樹深度越大,查找的次數(shù)越多,性能越差。 最壞的情況

    2024年04月25日
    瀏覽(31)
  • 為什么數(shù)據(jù)庫要允許沒有主鍵的表存在

    在數(shù)據(jù)庫設(shè)計(jì)中,主鍵是一個(gè)關(guān)鍵概念,用于唯一標(biāo)識(shí)數(shù)據(jù)庫表中的每一行數(shù)據(jù)。然而,有時(shí)候數(shù)據(jù)庫允許沒有主鍵的表存在的情況,這可能會(huì)引起一些爭議和疑問。本文將探討為什么數(shù)據(jù)庫允許沒有主鍵的表以及相關(guān)的考慮因素。 主鍵在數(shù)據(jù)庫中具有以下作用: 唯一標(biāo)識(shí)

    2024年02月08日
    瀏覽(32)
  • 為什么 volatile不能保證原子性

    volatile 本質(zhì)上是一種內(nèi)存屏障,它可以確保在 volatile 變量寫操作和讀操作之間不會(huì)發(fā)生重排序,這樣就可以保證對 volatile 變量的修改能夠立即對其他線程可見。但是, volatile 只能保證可見性,并不能保證原子性。 在 Java 中,原子性是指一個(gè)操作是不可中斷的,即使在

    2024年02月15日
    瀏覽(28)
  • 為什么sessionStorage不能代替vuex

    Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。 譯為“會(huì)話存儲(chǔ)”,也是HTML5新增的一個(gè)存儲(chǔ)對象, 用于本地臨時(shí)存儲(chǔ)同一窗口的數(shù)據(jù),在 關(guān)閉窗口之后 將會(huì)刪除這

    2024年02月09日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包