在innodb引擎下,如果创建表时没有显示地定义主键,则会按照下面的方式选择或创建主键

·首先判断表中是否有非空的唯一索引,如果有则为主键
·如果不符合上面条件,会自动创建一个6字节大小的指针( _rowid )

主键的选择时定义索引时的顺序,不是创建表的顺序

_rowid

  1. create table z(
  2. a int not null,
  3. b int null,
  4. c int not null,
  5. d int not null,
  6. unique key(b), unique key(d),unique(c));
  7. insert into z select 1,2,3,4;
  8. mysql> select _rowid from z;
  9. +--------+
  10. | _rowid |
  11. +--------+
  12. | 4 |
  13. +--------+
  14. 可以看出,三个唯一键bcd
  15. 主键为d ,因为b不是非空,c定义时在d之后
  16. _rowid 在联合主键下搜索不到

image.png

分区类型

详细见分区表