在《InnoDB存储引擎》书中看到InnoDB表都是有主键的,如果没有显示定义主键,则InnoDB首先判断表中是否有非空的唯一索引,如果有,该列即为主键。如果有多个单一列唯一索引,则按照唯一索引顺序,排在前的为主键。
-
mysql> show create table t2\\G
-
*************************** 1. row ***************************
-
Table: t2
-
Create Table: CREATE TABLE `t2` (
-
`a` int(11) DEFAULT NULL,
-
`b` int(11) NOT NULL,
-
`c` int(11) NOT NULL
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
在表t2上添加多个唯一索引,不按照列的顺序添加
-
mysql> alter table t2 add unique (a),add unique (c),add unique (b);
-
Query OK, 0 rows affected (0.09 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
mysql> show create table t2\\G
-
*************************** 1. row ***************************
-
Table: t2
-
Create Table: CREATE TABLE `t2` (
-
`a` int(11) DEFAULT NULL,
-
`b` int(11) NOT NULL,
-
`c` int(11) NOT NULL,
-
UNIQUE KEY `c` (`c`),
-
UNIQUE KEY `b` (`b`),
-
UNIQUE KEY `a` (`a`)
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
现在唯一索引的顺序是a,c,b,从建表语句就已经可以看出顺序了,现在唯一索引的顺序变成了c,b,a,插入数据
-
mysql> insert into t2 (b,c) values (1,2);
-
Query OK, 1 row affected (0.01 sec)
-
-
mysql> insert into t2 (b,c) values (10,20);
-
Query OK, 1 row affected (0.00 sec)
-
-
mysql> insert into t2 (a,b,c) values (10,20,30);
-
Query OK, 1 row affected (0.00 sec)
-
-
mysql> select a,b,c,_rowid from t2;
-
+------+----+----+--------+
-
| a | b | c | _rowid |
-
+------+----+----+--------+
-
| NULL | 1 | 2 | 2 |
-
| NULL | 10 | 20 | 20 |
-
| 10 | 20 | 30 | 30 |
-
+------+----+----+--------+
-
3 rows in set (0.00 sec)
_rowid列是InnoDB存储引擎自动创建的一个6字节大小的指针,此时可看出_rowid是以c列为主键,因选择主键的条件是非空的唯一索引,之后才按照唯一索引的顺序来选择主键。
如果表中无显示主键,也没有非空的唯一索引,那么主键是如何定义的呢?
-
mysql> create table t3(a int,b int,c int);
-
Query OK, 0 rows affected (0.06 sec)
-
-
mysql> insert into t3 select 1,2,3;
-
Query OK, 1 row affected (0.01 sec)
-
Records: 1 Duplicates: 0 Warnings: 0
-
-
mysql> insert into t3 select 2,3,4
-
-> ;
-
Query OK, 1 row affected (0.00 sec)
-
Records: 1 Duplicates: 0 Warnings: 0
-
-
mysql> insert into t3 select 3,4,5;
-
Query OK, 1 row affected (0.00 sec)
-
Records: 1 Duplicates: 0 Warnings: 0
创建表t3,并插入数据,且数据没有null值,但是列a,b,c都是可为null的列
-
mysql> select a,b,c,_rowid from t3\\G
-
ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
此时表中没有自动生成_rowid列。下面更改b列为not null
-
mysql> alter table t3 modify b int(11) not null;
-
Query OK, 0 rows affected (0.10 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
mysql> show create table t3\\G
-
*************************** 1. row ***************************
-
Table: t3
-
Create Table: CREATE TABLE `t3` (
-
`a` int(11) DEFAULT NULL,
-
`b` int(11) NOT NULL,
-
`c` int(11) DEFAULT NULL
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
-
-
mysql> select a,b,c,_rowid from t3\\G
-
ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
更改列为not null依然没有自动生成_rowid列。再次为c列添加可为空的唯一索引
-
mysql> alter table t3 add unique (c);
-
Query OK, 0 rows affected (0.10 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
mysql> select a,b,c,_rowid from t3\\G
-
ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
-
mysql> show create table t3\\G
-
*************************** 1. row ***************************
-
Table: t3
-
Create Table: CREATE TABLE `t3` (
-
`a` int(11) DEFAULT NULL,
-
`b` int(11) NOT NULL,
-
`c` int(11) DEFAULT NULL,
-
UNIQUE KEY `c` (`c`)
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
-
-
mysql> select a,b,c,_rowid from t3\\G
-
ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
也就是说如果表中没有显示指定主键,非空且唯一索引是必须条件。
如果表中有一个多列唯一索引,情况又将如何呢?
-
mysql> alter table t3 modify c int(11) not null;
-
Query OK, 0 rows affected, 1 warning (0.11 sec)
-
Records: 0 Duplicates: 0 Warnings: 1
-
-
mysql> alter table t3 add unique (b,c);
-
Query OK, 0 rows affected (0.10 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
mysql> show create table t3\\G
-
*************************** 1. row ***************************
-
Table: t3
-
Create Table: CREATE TABLE `t3` (
-
`a` int(11) DEFAULT NULL,
-
`b` int(11) NOT NULL,
-
`c` int(11) NOT NULL,
-
UNIQUE KEY `b` (`b`,`c`)
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1
-
1 row in set (0.00 sec)
-
-
mysql> select a,b,c,_rowid from t3\\G
-
ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
总结:
从我这里的实验可以看出,如果没有显示定义主键,单一列非空的唯一索引即为主键。
那么InnoDB存储引擎中所有表都有主键,该如何去查看呢?
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12219480/viewspace-1733787/
,如需转载,请注明出处,否则将追究法律责任。