【发布时间】:2017-10-24 08:19:06
【问题描述】:
当我从我的 mysql 表中删除所有记录时,自动增量计数器重置为 0。我的应用程序逻辑是将记录从 MySQL 迁移到其他分析引擎,然后从 MySQL 中删除迁移的记录。分析引擎使用与 MySQL 相同的主键管理记录,因此当 MySQL 重置计数器时,我的分析引擎中出现重复键错误。我希望 Mysql 保留 auto_increment,即使我从数据库中删除了所有记录。
alter table test auto_increment = 100;
我可以在删除我的记录后执行上述查询以保留 auto_increment,但如果我重新启动 MySQL,此计数器再次重置为默认值 0。
我知道这是 MySQL 的预期行为。但我正在寻找一种解决方法来克服它。
我正在使用 mariadb 而不是 mysql。重启前后请查看 auto_increment 计数器。
MariaDB [MY_DB]> SHOW TABLE STATUS FROM `MY_DB` WHERE `name` LIKE 'test' ;
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| test | InnoDB | 10 | Compact | 6 | 2730 | 16384 | 0 | 0 | 0 | 7 | 2017-05-22 23:38:06 | NULL | NULL | utf8_bin | NULL | | |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
1 row in set (0.00 sec)
MariaDB [MY_DB]> select max(id) from test;
+---------------+
| max(id) |
+---------------+
| 6 |
+---------------+
1 row in set (0.00 sec)
MariaDB [MY_DB]> delete from test where id in (1,2,3,4,5,6);
Query OK, 6 rows affected (0.00 sec)
MariaDB [MY_DB]> SHOW TABLE STATUS FROM `MY_DB` WHERE `name` LIKE 'test' ;
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| test | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 0 | 0 | 7 | 2017-05-22 23:38:06 | NULL | NULL | utf8_bin | NULL | | |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
1 row in set (0.00 sec)
MariaDB [MY_DB]> exit
Bye
root@atd-3000:~# systemctl restart mariadb
root@atd-3000:~# mysql -p***** MY_DB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 57
Server version: 10.1.17-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [MY_DB]> SHOW TABLE STATUS FROM `MY_DB` WHERE `name` LIKE 'test' ;
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| test | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 0 | 0 | 1 | 2017-05-22 23:38:06 | NULL | NULL | utf8_bin | NULL | | |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
【问题讨论】:
-
DELETE 查询不会重置 auto_increment id 而 TRUNCATE 会
-
发布您用来删除所有迁移行的查询
-
你是对的。但是如果你重新启动 mysql,那么它会将 auto_increment 计数器重置回 1。下面是我正在使用的删除查询:
Delete from test where id in (1,2,3,4,5) -
不,不会!?!?
标签: mysql mariadb auto-increment