這是一個(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ī)范。文章來源:http://www.zghlxwxcb.cn/news/detail-634841.html
題外話:唯一索引和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)!