Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I just want my mysql table id(primary key) to start from 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?

Edit1

I also emptied my table with truncate option

like image 385
user2234992 Avatar asked Apr 26 '13 08:04

user2234992


2 Answers

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.)

Reference

Reference 2

like image 126
Hanky Panky Avatar answered Nov 10 '22 02:11

Hanky Panky


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

More information: auto_increment_offset, How to set AUTO_INCREMENT step.

like image 4
Devart Avatar answered Nov 10 '22 03:11

Devart