Mysql 5.1.42 alter table auto_increment = 0 不起作用,truncate 起作用

发布于 2024-08-17 06:24:51 字数 440 浏览 2 评论 0原文

对于我的自动验收测试,我希望插入以 id=1 开头。 我在一台PC(XP 32位,mysql 5.1.something)上通过(从表中删除所有行后)“alter table tableName auto_increment = 0”实现了这一点。

我现在正在设置一台新电脑(Windows 7 64位,mysql 5.1.42),这个命令似乎没有效果。 我可以在 information_schema.tables 表中看到 auto_increment 值没有改回 0 --- 它只是不断上升。如果我尝试直接更改该表中的值,我会被告知“root”@“localhost”的访问被拒绝。这是否暗示了我的问题?

其他 stackoverflow 人员建议“从 tableName 截断”是一个不错的选择。我很高兴地报告这有效。但有谁知道为什么“alter table”命令不会重置auto_increment?

谢谢!

For my automated acceptance tests, I want inserts to start with id=1.
I achieved this on one PC (XP 32bit, mysql 5.1.something) with (after deleting all rows from the table), "alter table tableName auto_increment = 0".

I'm now setting up a new PC (Windows 7 64bit, mysql 5.1.42), and this command seems to have no effect.
I can see in the information_schema.tables table that the auto_increment value is not changed back to 0 --- it just keeps going up. If I try to change the value in that table directly, I'm told that access is denied to 'root'@'localhost'. Does this perhaps give a hint to my problem?

Other stackoverflow people had suggested that "truncate from tableName" is a good alternative. I'm happy to report that this works. But does anyone know why the "alter table" command won't reset the auto_increment?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本宫微胖 2024-08-24 06:24:51

不知道为什么它在一台服务器上工作,而在另一台服务器上不起作用,但是 MySQL 手册指出 (引用,强调我的)

更改值
要使用的 AUTO_INCRMENT 计数器
对于新行,请执行以下操作:

ALTER TABLE t2 AUTO_INCRMENT = 值;

您不能将计数器重置为小于或等于任何值
已被使用

对于
MyISAM,如果该值小于或
等于当前最大值
AUTO_INCRMENT 列中,
值重置为当前最大值
加一。
对于 InnoDB,如果该值
小于当前最大值
在该列中,没有发生错误,并且
当前序列值不变。

也许这就是问题的原因:您试图将 auto_increment 计数器设置回 0,但它已经高于该值 - 并且您无法将其重置为小于任何值的值已经用过了,没用。

NOt sure why it worked on one server, and doesn't work on the other, but the MySQL manual states (quoting, emphasis mine) :

To change the value of the
AUTO_INCREMENT counter to be used
for new rows, do this:

ALTER TABLE t2 AUTO_INCREMENT = value;

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.

Maybe that's the cause of the problem : you are trying to put the auto_increment counter back to 0, but it's already higher than that value -- and as you cannot reset it to a value that's less than any value that's already been used, it doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文