views:

174

answers:

3

Is there any to get the an AUTO_INCREMENT field of a InnoDB to start counting from 0 not 1

CREATE TABLE `df_mainevent` (
  `idDf_MainEvent` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`idDf_MainEvent`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+1  A: 
CREATE TABLE `df_mainevent` (
  `idDf_MainEvent` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`idDf_MainEvent`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;

works with MySQL >= 5.0.3.

EDIT:

Just noticed that MySQL in general does not like auto-increment values equal to 0 - that's independent from the used storage engine. MySQL just uses 1 as the first auto-increment value. So to answer the question: NO that's not possible but it does not depend on the storage engine.

Stefan Gehrig
+2  A: 

MySQL documentation:

If a user specifies NULL or 0 for the AUTO_INCREMENT column in an INSERT, InnoDB treats the row as if the value had not been specified and generates a new value for it.

So it means that 0 is a 'special' value which is similar to NULL. Even when you use AUTO_INCREMENT = 0 is will set the initial value to 1.

Beginning with MySQL 5.0.3, InnoDB supports the AUTO_INCREMENT = N table option in CREATE TABLE and ALTER TABLE statements, to set the initial counter value or alter the current counter value. The effect of this option is canceled by a server restart, for reasons discussed earlier in this section.

Lukasz Lysik
A: 

I have not been able to have autoincrement start at 0, but starting at 1 and then setting it to 0 via an UPDATE works fine.

I commonly use this trick to detect deletes in a table.

On update of any row, I set that row's last update time.

On deletes, I set the last update time of row 0.

Daren Schwenke