MySQL 自动递增?

标签 mysql auto-increment

我有 2 个不同的 MySQL 服务器。 一个是 5.6.12,另一个是 5.5.25a。

在5.5版本的服务器中我可以这样做:

ALTER TABLE AUTO_INCREMENT = 100;
ALTER TABLE AUTO_INCREMENT = 50;

5.6 版本只允许我将 auto_increment 更改为比当时的 auto_increment 更高的值。

当然,我并不是要将值更改为低于具有最大值的记录的值。

两台MySQL服务器都是InnoDB

我需要在最新版本中使用那个commen

有什么线索吗?

最佳答案

此查询适用于 mysql 5.6.12 和 5.5.25:

ALTER TABLE  `table_name` AUTO_INCREMENT =10

根据 mysql manual :

You cannot reset the counter to a value less than or equal to any that have already been used. For MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum plus one. For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.

如果表没有记录,可以使用TRUNCATE:

TRUNCATE TABLE table_name

来自 mysql manual :

Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.


更新:mysql5.6.12 似乎有一些错误:http://bugs.mysql.com/bug.php?id=69882

关于MySQL 自动递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18418471/

相关文章:

java - Glassfish 不创建 JDBC 资源

php - 检查数据库架构的更改并更新

mysql - 在插入中立即使用自动增量值

sql-server - 无法通过 SQL Server Express Management Studio 设置自动增量?

java - Oracle自动增量与序列和触发器问题

mysql - 警告 : ? : (mysql. W002)

mysql - 错误 1045 (28000) : Access denied for user 'ODBC' @'localhost' (using password: YES)

javascript - 将mysql查询返回的数据转换为json(基于树)

c# - 如何使用 EF 核心在 SQLite 中创建自动增量列?

MySQL - 生成唯一的ID,如自动递增