InnoDB表主键的选择

在《InnoDB存储引擎》书中看到InnoDB表都是有主键的,如果没有显示定义主键,则InnoDB首先判断表中是否有非空的唯一索引,如果有,该列即为主键。如果有多个单一列唯一索引,则按照唯一索引顺序,排在前的为主键。

  1. mysql> show create table t2\\G
  2. *************************** 1. row ***************************
  3.        Table: t2
  4. Create Table: CREATE TABLE `t2` (
  5.   `a` int(11) DEFAULT NULL,
  6.   `b` int(11) NOT NULL,
  7.   `c` int(11) NOT NULL
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  9. 1 row in set (0.00 sec)
在表t2上添加多个唯一索引,不按照列的顺序添加

  1. mysql> alter table t2 add unique (a),add unique (c),add unique (b);
  2. Query OK, 0 rows affected (0.09 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0

  4. mysql> show create table t2\\G
  5. *************************** 1. row ***************************
  6.        Table: t2
  7. Create Table: CREATE TABLE `t2` (
  8.   `a` int(11) DEFAULT NULL,
  9.   `b` int(11) NOT NULL,
  10.   `c` int(11) NOT NULL,
  11.   UNIQUE KEY `c` (`c`),
  12.   UNIQUE KEY `b` (`b`),
  13.   UNIQUE KEY `a` (`a`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  15. 1 row in set (0.00 sec)
现在唯一索引的顺序是a,c,b,从建表语句就已经可以看出顺序了,现在唯一索引的顺序变成了c,b,a,插入数据

  1. mysql> insert into t2 (b,c) values (1,2);
  2. Query OK, 1 row affected (0.01 sec)

  3. mysql> insert into t2 (b,c) values (10,20);
  4. Query OK, 1 row affected (0.00 sec)

  5. mysql> insert into t2 (a,b,c) values (10,20,30);
  6. Query OK, 1 row affected (0.00 sec)

  7. mysql> select a,b,c,_rowid from t2;
  8. +------+----+----+--------+
  9. | a | b | c | _rowid |
  10. +------+----+----+--------+
  11. | NULL | 1 | 2 | 2 |
  12. | NULL | 10 | 20 | 20 |
  13. | 10 | 20 | 30 | 30 |
  14. +------+----+----+--------+
  15. 3 rows in set (0.00 sec)
_rowid列是InnoDB存储引擎自动创建的一个6字节大小的指针,此时可看出_rowid是以c列为主键,因选择主键的条件是非空的唯一索引,之后才按照唯一索引的顺序来选择主键。
如果表中无显示主键,也没有非空的唯一索引,那么主键是如何定义的呢?

  1. mysql> create table t3(a int,b int,c int);
  2. Query OK, 0 rows affected (0.06 sec)

  3. mysql> insert into t3 select 1,2,3;
  4. Query OK, 1 row affected (0.01 sec)
  5. Records: 1 Duplicates: 0 Warnings: 0

  6. mysql> insert into t3 select 2,3,4
  7.     -> ;
  8. Query OK, 1 row affected (0.00 sec)
  9. Records: 1 Duplicates: 0 Warnings: 0

  10. mysql> insert into t3 select 3,4,5;
  11. Query OK, 1 row affected (0.00 sec)
  12. Records: 1 Duplicates: 0 Warnings: 0
创建表t3,并插入数据,且数据没有null值,但是列a,b,c都是可为null的列

  1. mysql> select a,b,c,_rowid from t3\\G
  2. ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
此时表中没有自动生成_rowid列。下面更改b列为not null

  1. mysql> alter table t3 modify b int(11) not null;
  2. Query OK, 0 rows affected (0.10 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0

  4. mysql> show create table t3\\G
  5. *************************** 1. row ***************************
  6.        Table: t3
  7. Create Table: CREATE TABLE `t3` (
  8.   `a` int(11) DEFAULT NULL,
  9.   `b` int(11) NOT NULL,
  10.   `c` int(11) DEFAULT NULL
  11. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  12. 1 row in set (0.00 sec)

  13. mysql> select a,b,c,_rowid from t3\\G
  14. ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
更改列为not null依然没有自动生成_rowid列。再次为c列添加可为空的唯一索引

  1. mysql> alter table t3 add unique (c);
  2. Query OK, 0 rows affected (0.10 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0

  4. mysql> select a,b,c,_rowid from t3\\G
  5. ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
  6. mysql> show create table t3\\G
  7. *************************** 1. row ***************************
  8.        Table: t3
  9. Create Table: CREATE TABLE `t3` (
  10.   `a` int(11) DEFAULT NULL,
  11.   `b` int(11) NOT NULL,
  12.   `c` int(11) DEFAULT NULL,
  13.   UNIQUE KEY `c` (`c`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  15. 1 row in set (0.00 sec)

  16. mysql> select a,b,c,_rowid from t3\\G
  17. ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
也就是说如果表中没有显示指定主键,非空且唯一索引是必须条件。
如果表中有一个多列唯一索引,情况又将如何呢?

  1. mysql> alter table t3 modify c int(11) not null;
  2. Query OK, 0 rows affected, 1 warning (0.11 sec)
  3. Records: 0 Duplicates: 0 Warnings: 1

  4. mysql> alter table t3 add unique (b,c);
  5. Query OK, 0 rows affected (0.10 sec)
  6. Records: 0 Duplicates: 0 Warnings: 0

  7. mysql> show create table t3\\G
  8. *************************** 1. row ***************************
  9.        Table: t3
  10. Create Table: CREATE TABLE `t3` (
  11.   `a` int(11) DEFAULT NULL,
  12.   `b` int(11) NOT NULL,
  13.   `c` int(11) NOT NULL,
  14.   UNIQUE KEY `b` (`b`,`c`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  16. 1 row in set (0.00 sec)

  17. mysql> select a,b,c,_rowid from t3\\G
  18. ERROR 1054 (42S22): Unknown column \'_rowid\' in \'field list\'
总结:
从我这里的实验可以看出,如果没有显示定义主键,单一列非空的唯一索引即为主键。
那么InnoDB存储引擎中所有表都有主键,该如何去查看呢?

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12219480/viewspace-1733787/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12219480/viewspace-1733787/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值