更改 MySQL 中自动增量值的当前计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1476512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:10:50  来源:igfitidea点击:

Changing the current count of an Auto Increment value in MySQL?

mysql

提问by coderama

Currently every time I add an entry to my database, the auto increment value increments by 1, as it should. However, it is only at a count of 47. So, if I add a new entry, it will be 48, and then another it will be 49 etc.

目前,每次我向我的数据库添加一个条目时,自动增量值都会增加 1,这是应该的。然而,它只有 47 个。所以,如果我添加一个新条目,它将是 48,然后另一个它将是 49,等等。

I want to change what the current Auto Increment counter is at. I.e. I want to change it from 47 to say, 10000, so that the next value entered, will be 10001. How do I do that?

我想更改当前自动增量计数器的位置。即我想将它从 47 更改为 10000,以便输入的下一个值将是 10001。我该怎么做?

回答by Pascal MARTIN

You can use ALTER TABLEto set the value of an AUTO_INCREMENT column ; quoting that page :

您可以使用ALTER TABLE设置 AUTO_INCREMENT 列的值;引用该页面:

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

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

ALTER TABLE t2 AUTO_INCREMENT = value;

There is also a note saying that :

还有一张纸条说:

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_INCREMENTcolumn, 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.

您不能将计数器重置为小于或等于任何已使用值的值。
对于 MyISAM,如果该值小于或等于当前AUTO_INCREMENT列中的最大值 ,则将该值重置为当前最大值加一。
对于 InnoDB,如果该值小于列中当前的最大值,则不会发生错误并且不会更改当前序列值。

Hope this helps !

希望这可以帮助 !

回答by Paul Dixon

See manual for ALTER TABLE- this should do it:

请参阅ALTER TABLE手册- 这应该这样做:

ALTER TABLE [tablename] AUTO_INCREMENT = [number]

回答by Crickey

you can get that done by executing the following statement

你可以通过执行以下语句来完成

ALTER TABLE t2 AUTO_INCREMENT = 10000;

So next Auto Increment key will start from the 10001.

所以下一个 Auto Increment 键将从 10001 开始。

I hope this will solve the problem

我希望这能解决问题

回答by John Miller

You can also set it with the table creation statement as follows;

您也可以使用表创建语句进行设置,如下所示;

CREATE TABLE mytable (
     id int NOT NULL AUTO_INCREMENT,
     ...
     PRIMARY KEY (ID)
)AUTO_INCREMENT=10000;

Hope it helps someone.

希望它可以帮助某人。