简体   繁体   English

MySQL的AUTO_INCREMENT列是如何重置的?

[英]How is MySQL's AUTO_INCREMENT column reset

Few question about resetting MySQL's AUTO_INCREMENT column. 关于重置MySQL的AUTO_INCREMENT列的几个问题。

First without deleting/creating a table, or deleting rows via TRUNCATE TABLE (see footer for that code) -- is this the correct way to do it? 首先不删除/创建表,或通过TRUNCATE TABLE删除行(请参阅该代码的页脚) - 这是正确的方法吗?

ALTER TABLE [insert_TABLE_name] AUTO_INCREMENT = 1

Next, is value set to 0 or 1 -- and why? 接下来,将值设置为01 - 为什么?

Bonus, any extra advice. 奖金,任何额外的建议。

It's set to whatever you use in the alter table query, which is 1 in your case. 它设置为您在alter table查询中使用的任何内容,在您的情况下为1 However, since you're not deleting rows, you've now probably set yourself up for a primary key violation, since your next inserted row will try to use that 1 and conflict with a previously created row with the same key. 但是,由于您没有删除行,您现在可能已经设置了主键违规,因为您的下一个插入行将尝试使用该1并与先前创建的具有相同键的行冲突。

You cannot reset the counter to a value less than or equal to any that have already been used. 您不能将计数器重置为小于或等于任何已使用的值。

you are trying to put the auto_increment counter back to 1, 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. 你试图将auto_increment计数器恢复为1,但它已经高于该值,并且由于你无法将其重置为小于已经使用过的任何值的值,它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM