How to add AUTO_INCREMENT to an existing column?

MysqlAuto Increment

Mysql Problem Overview


How do I add auto_increment to an existing column of a MySQL table?

Mysql Solutions


Solution 1 - Mysql

I think you want to MODIFY the column as described for the ALTER TABLE command. It might be something like this:

ALTER TABLE users MODIFY id INTEGER NOT NULL AUTO_INCREMENT;

Before running above ensure that id column has a Primary index.

Solution 2 - Mysql

Method to add AUTO_INCREMENT to a table with data while avoiding “Duplicate entry” error:

  1. Make a copy of the table with the data using INSERT SELECT:

    CREATE TABLE backupTable LIKE originalTable; 
    INSERT backupTable SELECT * FROM originalTable;
    
  2. Delete data from originalTable (to remove duplicate entries):

    TRUNCATE TABLE originalTable;
    
  3. To add AUTO_INCREMENT and PRIMARY KEY

    ALTER TABLE originalTable ADD id INT PRIMARY KEY AUTO_INCREMENT;
    
  4. Copy data back to originalTable (do not include the newly created column (id), since it will be automatically populated)

    INSERT originalTable (col1, col2, col3) 
    SELECT col1, col2,col3
    FROM backupTable;
    
  5. Delete backupTable:

    DROP TABLE backupTable;
    

I hope this is useful!

More on the duplication of tables using CREATE LIKE:

https://stackoverflow.com/questions/3280006/duplicating-a-mysql-table-indexes-and-data

Solution 3 - Mysql

Alter table table_name modify column_name datatype(length) AUTO_INCREMENT PRIMARY KEY

You should add primary key to auto increment, otherwise you got error in mysql.

Solution 4 - Mysql

Simply just add auto_increment Constraint In column or MODIFY COLUMN :-

 ALTER TABLE `emp` MODIFY COLUMN `id` INT NOT NULL UNIQUE AUTO_INCREMENT FIRST;

Or add a column first then change column as -

1. Alter TABLE `emp` ADD COLUMN `id`;

2. ALTER TABLE `emp` CHANGE COLUMN `id` `Emp_id` INT NOT NULL UNIQUE AUTO_INCREMENT FIRST;

Solution 5 - Mysql

This worked for me in case you want to change the AUTO_INCREMENT-attribute for a not-empty-table:

1.)Exported the whole table as .sql file
2.)Deleted the table after export
2.)Did needed change in CREATE_TABLE command
3.)Executed the CREATE_TABLE and INSERT_INTO commands from the .sql-file
...et viola

Solution 6 - Mysql

I managed to do this with the following code:

ALTER TABLE `table_name`
CHANGE COLUMN `colum_name` `colum_name` INT(11) NOT NULL AUTO_INCREMENT FIRST;

This is the only way I could make a column auto increment.

INT(11) shows that the maximum int length is 11, you can skip it if you want.

Solution 7 - Mysql

Alter table table_name modify table_name.column_name data_type AUTO_INCREMENT;

eg:

Alter table avion modify avion.av int AUTO_INCREMENT;

Solution 8 - Mysql

if you have FK constraints and you don't want to remove the constraint from the table. use "index" instead of primary. then you will be able to alter it's type to auto increment

Solution 9 - Mysql

I had existing data in the first column and they were 0's. First I made the first column nullable. Then I set the data for the column to null. Then I set the column as an index. Then I made it a primary key with auto incrementing turned on. This is where I used another persons answer above:

ALTER TABLE `table_name` CHANGE COLUMN `colum_name` `colum_name` INT(11) NOT NULL AUTO_INCREMENT FIRST;

This Added numbers to all the rows of this table starting at one. If I ran the above code first it wasn't working because all the values were 0's. And making it an index was also required before making it auto incrementing. Next I made the column a primary key.

Solution 10 - Mysql

This worked in my case , if you want to change the column attribute to auto-increment which is already having some data

1.GO to structure, select the column to want to change. 2.After selecting the column , choose primary key from the options below. [1]: https://i.stack.imgur.com/r7w8f.png 3.Then change the column attribute to auto-increment using alter method

Solution 11 - Mysql

ALTER TABLE Table name ADD column datatype AUTO_INCREMENT,ADD primary key(column);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - MysqlDon KirkbyView Answer on Stackoverflow
Solution 2 - MysqlArian AcostaView Answer on Stackoverflow
Solution 3 - MysqlphpView Answer on Stackoverflow
Solution 4 - MysqlZigri2612View Answer on Stackoverflow
Solution 5 - MysqlwaltView Answer on Stackoverflow
Solution 6 - MysqlFluchView Answer on Stackoverflow
Solution 7 - MysqlMariaView Answer on Stackoverflow
Solution 8 - Mysqluser4626745View Answer on Stackoverflow
Solution 9 - MysqlPHPGuruView Answer on Stackoverflow
Solution 10 - Mysqldevika_1View Answer on Stackoverflow
Solution 11 - MysqlGopal MehakareView Answer on Stackoverflow