mysql的主键索引为什么不能null

这篇具有很好参考价值的文章主要介绍了mysql的主键索引为什么不能null。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


这是一个非常奇怪且有趣的问题。可以通过官方文档进行解读

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.

从这个里面我们可以得出答案,null暗示着数据的缺失,对null的算术运算和相等测试得到的结果还会是一个null。null有点类似IEEE 中浮点数的 NAN not a number的概念(其实有很多数都是NAN)。
从这里我们也可以看出: null == null的结果为 null 而不是 true 是缺失的,可以认为null 不是唯一的,就像 IEEE的浮点数 NAN一样,不是只有一个值的是一族值的总称。

这里还有一点提到了: null通常不被放进索引中,这也是为什么索引会因为null而失效。

再看官方文档对 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.

这里就很清晰了,主键索引是为了,唯一确定每一条数据,null因为缺失,不知道,所以不能唯一确定每一条数据(因为算术运算得到的是null而不是一个确定的值 而是一个null, 只能进行 IS NULL 和 not null进行运算)。

这里的设计其实也是为了符合规范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.

说白了也是要符合制定的规范。

题外话:唯一索引和null

唯一索引可以有null值,且可以有多个null值, 因为 null 跟 null进行相等的比较的时候,得到结果是 null。如果 null 跟 null进行相等比较的时候得到的结果是 相等的, 那么唯一索引可以拥有一个null值,而不是多个null值。文章来源地址https://www.toymoban.com/news/detail-634841.html

到了这里,关于mysql的主键索引为什么不能null的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • MySQl有哪些索引(种类)?索引特点?为什么要使用索引?

    普通索引:仅加速查询 唯一索引:加速查询 + 列值 唯一(可以有null) 主键索引:加速查询 + 列值 唯一(不可以有null)+ 表中只有一个 组合索引: 多列值组成一个索引 ,专门用于组合搜索,其效率大于索引合并 全文索引:对文本的内容进行分词,进行搜索 索引合并:使用

    2024年02月07日
    浏览(65)
  • MySQL为什么选择B+树创建索引

    将磁盘中存储的所有数据记录依次加载,与给定条件对比,直到找到目标记录; 类比数组结构的线性查找,效率较低; 结合数组和链表结构(或者树结构)存储数据; 通过哈希函数(散列函数)计算哈希地址,相同输入在固定函数下输出保持不变; 哈希结构会发生哈希冲突

    2024年02月13日
    浏览(44)
  • MySQL为什么采用B+树作为索引底层数据结构?

            索引就像一本书的目录,通过索引可以快速找到我们想要找的内容。那么什么样的数据结构可以用来实现索引呢?我们可能会想到:二叉查找树,平衡搜索树,或者是B树等等一系列的数据结构,那么为什么MySQL最终选择了B+树作为索引的数据结构呢?         要想

    2024年02月16日
    浏览(42)
  • MySQL为什么要使用B+树做索引?MySQL索引存储模型推演,B+树在MySQL的落地形式

    user_innodb这张表里有4个字段,id,name,gender,phone。 当这张表有500万条数据,在没有索引的name字段上执行一条where查询: 如果name字段上有索引呢?我们在name字段上面创建一个索引,再来执行一下查询: 我们再来执行一下select语句。 我们会发现,有索引的查询和没有索引的

    2024年02月16日
    浏览(49)
  • MSQL系列(十二) Mysql实战-为什么索引要建立在被驱动表上

    Mysql实战-为什么索引要建立在被驱动表上 前面我们讲解了B+Tree的索引结构,也详细讲解下 left Join的底层驱动表 选择原理,那么今天我们来看看到底如何用以及如何建立索引和索引优化 开始之前我们先提一个问题, 为什么索引要建立在被驱动表上 ? 1.建表及测试数据 我们先

    2024年02月08日
    浏览(41)
  • MySQL 索引为什么使用 B+ 树,而不使用红黑树 / B 树 ?

    首先 B 树和 B+ 树 都是多叉搜索树,然后我们先来观察一下 B+ 树和 B 树的数据结构: B+ 树的数据结构实现  B 树的数据结构实现 【B+ 树相较于 B 树的优势】 1. IO 次数更少(查询效率更高)         B+ 树的非叶子节点不存放实际的数据,仅存放索引,因此数据量相同的情况

    2024年02月12日
    浏览(36)
  • MySQL索引为什么选择B+树,而不是二叉树、红黑树、B树?

    二叉树是一种二分查找树,有很好的查找性能,相当于二分查找。 二叉树的非叶子节值大于左边子节点、小于右边子节点。 原因: 但是当N比较大的时候,树的深度比较高。数据查询的时间主要依赖于磁盘IO的次数,二叉树深度越大,查找的次数越多,性能越差。 最坏的情况

    2024年04月25日
    浏览(36)
  • 为什么数据库要允许没有主键的表存在

    在数据库设计中,主键是一个关键概念,用于唯一标识数据库表中的每一行数据。然而,有时候数据库允许没有主键的表存在的情况,这可能会引起一些争议和疑问。本文将探讨为什么数据库允许没有主键的表以及相关的考虑因素。 主键在数据库中具有以下作用: 唯一标识

    2024年02月08日
    浏览(58)
  • 为什么 volatile不能保证原子性

    volatile 本质上是一种内存屏障,它可以确保在 volatile 变量写操作和读操作之间不会发生重排序,这样就可以保证对 volatile 变量的修改能够立即对其他线程可见。但是, volatile 只能保证可见性,并不能保证原子性。 在 Java 中,原子性是指一个操作是不可中断的,即使在

    2024年02月15日
    浏览(47)
  • 为什么sessionStorage不能代替vuex

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。 译为“会话存储”,也是HTML5新增的一个存储对象, 用于本地临时存储同一窗口的数据,在 关闭窗口之后 将会删除这

    2024年02月09日
    浏览(59)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包