谈到MySQL数据库表设计和SQL优化,都会了解要有索引,可以说MySQL就是索引的代名词。这话一点都不夸张。MySQL数据库底层的innodb引擎就是索引组织表。数据行的所有操作都是基本主键进行的。
数据库中定义的主键具备如下特性:
1、任何两行都不具有相同的主键值,保证唯一性。
2、每个行都必须具有一个主键值(主键列不允许NULL值)
目前来说MySQL的处理逻辑都是跟主键绑在一起。主键的重要度不言而喻。
官方说明
表中每一行都应该有可以唯一标识自己的一列,当没有设置主键的时候MySQL本身会生成隐藏的列做为主键。
- When you define a PRIMARY KEY on your table, InnoDB uses it as the clustered index. Define a primary key for each table that you create. If there is no logical unique and non-null column or set of columns, add a new auto-increment column, whose values are filled in automatically.
当表上定义一个主键时,InnoDB使用它作为聚集索引。如果没有逻辑惟一的非空列或列集,则建议添加一个新的自动递增列,其值将自动填充。
- If you do not define a PRIMARY KEY for your table, MySQL locates the first UNIQUE index where all the key columns are NOT NULL and InnoDB uses it as the clustered index.
如果没有为表定义一个主键,MySQL定位第一个UNIQUE索引,所有的键列都不是NULL, InnoDB使用它作为聚集索引。
......