Mysql - 如何设置自动增量从零开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16232430/
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 17:28:08  来源:igfitidea点击:

Mysql - how to set auto-increment to start from zero

mysqlprimary-key

提问by user2234992

I just want my mysql table id(primary key) to start from 0..

我只希望我的 mysql 表 ID(主键)从 0..

As I have seen, I used ALTER TABLE yourtable AUTO_INCREMENT =0 but it starts from 1..What is that I need to do?

正如我所见,我使用了 ALTER TABLE yourtable AUTO_INCREMENT =0 但它从 1 开始......我需要做什么?

Edit1

编辑1

I also emptied my table with truncate option

我还使用 truncate 选项清空了我的表

回答by Hanky Panky

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO'

NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.

This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.)

NO_AUTO_VALUE_ON_ZERO 影响 AUTO_INCREMENT 列的处理。通常,您通过向列中插入 NULL 或 0 来生成列的下一个序列号。NO_AUTO_VALUE_ON_ZERO 为 0 抑制此行为,以便只有 NULL 生成下一个序列号。

如果 0 已存储在表的 AUTO_INCREMENT 列中,则此模式很有用。(顺便说一下,存储 0 不是推荐的做法。)

Reference

参考

Reference 2

参考文献2

回答by Devart

You cannot force this to be zero. This is limited by auto_increment_offsetserver variable. The default value is 1, the range is [1 .. 65535].

你不能强迫它为零。这受auto_increment_offset服务器变量的限制。默认值为 1,范围为 [1 .. 65535]。

More information: auto_increment_offset, How to set AUTO_INCREMENT step.

更多信息:auto_increment_offset如何设置 AUTO_INCREMENT step