Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql can't make column auto_increment

I have a table "Bestelling" with 4 columns: "Id" (PK), "KlantId", "Datum", "BestellingsTypeId", now I want to make the column Id auto_increment, however, when I try to do that, I get this error:

ERROR 1062: ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'  SQL Statement:  ALTER TABLE `aafest`.`aafest_bestelling` CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT    ERROR: Error when running failback script. Details follow.    ERROR 1046: No database selected  SQL Statement:  CREATE TABLE `aafest_bestelling` (    `Id` int(11) NOT NULL,    `KlantId` int(11) DEFAULT NULL,    `Datum` date DEFAULT NULL,    `BestellingstypeId` int(11) DEFAULT NULL,    PRIMARY KEY (`Id`)  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 

Anyone got an idea?

like image 972
Sander Declerck Avatar asked Mar 23 '11 09:03

Sander Declerck


People also ask

How do I make a column auto increment in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.

How do I add an auto increment to an existing column?

To change the value of the AUTO_INCREMENT the table must be using the MyISAM engine. So go to the table properties, change the engine from say InnoDB to MyISAM, then change the AUTO_INCREMENT value to whatever should be next in your sequence.

Why is auto increment disabled in MySQL Workbench?

This was likely due to that although "INT" had been entered for the "Data Type" box, MySQL Workbench hadn't updated the available field options & was still using UI logic for a non-INT column (where auto increment isn't available).

Can I auto increment two columns MySQL?

You can't have two auto-increment columns.


2 Answers

This will happen if the table contains an existing record with an id of 0 (or negative). Updating all existing records to use positive values will allow auto_increment to be set on that column.

Edit: Some people asked how that 0 got in there. For clarification, the MySQL Reference Manual states that "For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence." So, if you performed an insert on a table without providing a value for the numeric column before the auto_increment was enabled, then the default 0 would be used during the insert. More details may be found at https://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html.

like image 115
SystemParadox Avatar answered Sep 18 '22 22:09

SystemParadox


I also had this issue when trying to convert a column to auto_increment where one row had a value of 0. An alternative to changing the 0 value temporarily is via setting:

SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO'; 

for the session.

This allowed the column to be altered to auto_increment with the zero id in place.

The zero isn't ideal - and I also wouldn't recommend it being used in an auto_increment column. Unfortunately it's part of an inherited data set so I'm stuck with it for now.

Best to clear the setting (and any others) afterwards with:

SET SESSION sql_mode=''; 

although it will be cleared when the current client session clsoes.

Full details on the 'NO_AUTO_VALUE_ON_ZERO' setting here.

like image 24
Garoke Avatar answered Sep 20 '22 22:09

Garoke