part1:重启的影响
为包含表头和表体的业务对象创建表,要求表头.fid=表体.fid。有一种常见的思路是通过一个单独的表设置种子列,通过这个单独的表获取表头和表体需要的fid,获取之后删除单独表的记录。这里需要注意了,这个独立的表必须使用myisam格式,因为innodb在数据库重启之后,会重新组织auto_increment的下一个值。
测试过程如下:
mysql> create table t_myisam(col1 int primary key auto_increment ,col2 char(1)) engine=myisam;
Query OK, 0 rows affected (0.01 sec)
mysql> create table t_innodb(col1 int primary key auto_increment ,col2 char(1)) engine=innodb;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t_myisam(col2) values('a'),('b'),('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into t_innodb(col2) values('a'),('b'),('c');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> delete from t_myisam where col2='c';
Query OK, 1 row affected (0.00 sec)
mysql> delete from t_innodb where col2='c';
Query OK, 1 row affected (0.02 sec)
mysql> exit;
Bye
[root@localhost ~]# service mysqld restart;
Redirecting to /bin/systemctl restart mysqld.service
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> insert into t_myisam(col2) values('e');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_innodb(col2) values('e');
Query OK, 1 row affected (0.04 sec)
mysql> select * from t_myisam;
+------+------+
| col1 | col2 |
+------+------+
| 1 | a |
| 2 | b |
| 4 | e |
+------+------+
3 rows in set (0.00 sec)
mysql> select * from t_innodb;
+------+------+
| col1 | col2 |
+------+------+
| 1 | a |
| 2 | b |
| 3 | e |
+------+------+
3 rows in set (0.00 sec)
重启之后,系统检测t_innodb最大的id为2,下一个新增的id为3。而myisam类型的表则不受重启影响。
part2:对索引的依赖
auto_increment属性的列要求是索引或者复合索引的一部分。复合索引时,innodb存储引擎要求该列必须是复合索引的第一列,而myisam存储引擎则可以是其他列。当 auto_increment属性的列是复合索引非第一列时,这个序列跟我们常用的整表的序列是不同的,它是分组的序列。
mysql> create table t_myisam_mulindex(col1 int,col2 int auto_increment,col3 char(1),key(col1,col2)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_myisam_mulindex(col1,col3) values(1,'a'),(1,'b'),(1,'c'),(2,'d'),(2,'e'),(3,'g');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from t_myisam_mulindex;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 1 | 1 | a |
| 1 | 2 | b |
| 1 | 3 | c |
| 2 | 1 | d |
| 2 | 2 | e |
| 3 | 1 | g |
+------+------+------+
6 rows in set (0.00 sec)