mysq5.6的innodb之前添加主键需要copy tmp table,而mysql5.6现在可以在线添加主键,是不是很强大。
以下是我测试步骤:
CREATE TABLE `sbtest` (
`id` int(11) NOT NULL DEFAULT '0',
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
`id1` int(11) DEFAULT NULL,
KEY `k` (`k`)
) ENGINE=InnoDB
添加主键:
mysql> alter table sbtest add primary key(id);
在添加主键同时,执行下面操作,这些操作都能顺利执行,而不锁表:
mysql> select * from sbtest limit 1;
+----+---+---+----------------------------------------------------+------+
| id | k | c | pad | id1 |
+----+---+---+----------------------------------------------------+------+
| 1 | 0 | | qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt | NULL |
+----+---+---+----------------------------------------------------+------+
1 row in set (0.00 sec)
mysql> insert into sbtest(id) values(9999999);
Query OK, 1 row affected (0.02 sec)
mysql> update sbtest set id=9999998 where id=9999999;
Query OK, 1 row affected (11.81 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from sbtest where id=9999998;
Query OK, 1 row affected (13.43 sec)