How do I add auto_increment
to an existing column of a MySQL table?
12 Answers
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.

- 1,126
- 11
- 28

- 53,582
- 27
- 205
- 286
-
11Might also want to include the current column value as an offset, eg `ALTER TABLE tbl AUTO_INCREMENT = 100;` – Phil Feb 17 '11 at 23:20
-
20If you already have values in the table, you'll probably get a duplicate key error for value of 1. 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. Once you apply those changes you can switch the engine back to whatever you where using. – sday Oct 01 '11 at 02:25
-
https://stackoverflow.com/questions/1485668/how-to-set-initial-value-and-auto-increment-in-mysql – symcbean Jan 08 '19 at 12:34
Method to add AUTO_INCREMENT to a table with data while avoiding “Duplicate entry” error:
Make a copy of the table with the data using INSERT SELECT:
CREATE TABLE backupTable LIKE originalTable; INSERT backupTable SELECT * FROM originalTable;
Delete data from originalTable (to remove duplicate entries):
TRUNCATE TABLE originalTable;
To add AUTO_INCREMENT and PRIMARY KEY
ALTER TABLE originalTable ADD id INT PRIMARY KEY AUTO_INCREMENT;
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;
Delete backupTable:
DROP TABLE backupTable;
More on the duplication of tables using CREATE LIKE:

- 30,962
- 25
- 85
- 135

- 6,491
- 1
- 35
- 32
-
If you have a large table...or you want to do things a little smarter replace step 1 with: CREATE TABLE backupTable LIKE originalTable; RENAME TABLE originalTable TO originalTable.old, backupTable TO originalTable; Skip step 2 or you will loose all your data. – Timothy Martens Feb 24 '17 at 17:01
-
4This looks very dangerous to me, because the records could easily end up with different id numbers than they started with. If other tables have foreign keys to `originalTable` or you care about the id numbers for other reasons, then I don't think you want to use this method. – Don Kirkby Mar 29 '18 at 18:00
-
@DonKirkby Thanks for pointing that out. It is unlikely that a non-unique id column was used in conjunction with a foreign key, but you are right. This method precisely does that. It rebuilds (or creates) all the ids for them to be unique. – Arian Acosta Mar 29 '18 at 18:23
-
This is a problem because anything using the data during TRUNCATE will see unepected behavior – KeatsKelleher Nov 17 '18 at 15:18
-
If your key column already has data in it, and it's unique, and you're still getting this error, look for a record with id=0 . If you have one, remove that, or re-number it. Then the error will go away and you can just turn on auto-increment. Then it will use the existing values for that column without renumbering them. This happens because it tries to give the id=0 row an id=1 (because you can't have an auto-increment id of 0) which will conflict with an existing id=1 . – Joe May 20 '22 at 14:41
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.

- 170,088
- 45
- 397
- 571

- 4,307
- 1
- 24
- 13
-
7But if the column was already a primary key, you will get an error like "ERROR 1068 (42000): Multiple primary key defined". – Manish Bansal Dec 27 '18 at 10:29
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;

- 2,279
- 21
- 33
-
That `id` column needs an `INDEX`, but not necessarily `UNIQUE` or `PRIMARY KEY`. A plain `INDEX` would allow you to _manually_ add a dup value (which you won't do), but otherwise, it is just as good. – Rick James May 01 '19 at 17:44
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

- 101
- 5
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.

- 323
- 3
- 8
-
`FIRST` is a good convention, but it is not a requirement. The `(11)` does nothing unless you also have `ZEROFILL`; don't bother specifying it. – Rick James May 01 '19 at 17:45
Alter table table_name modify table_name.column_name data_type AUTO_INCREMENT;
eg:
Alter table avion modify avion.av int AUTO_INCREMENT;
-
Welcome to SO, your answer already exists. Please format your source code and explain! – cSteusloff Apr 17 '18 at 07:14
-
This answer is worked for me on existing table which has primary key column constraint already . – MdFarzan Jan 14 '22 at 07:25
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

- 11
- 6
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.

- 398
- 3
- 9
-
You could have used `SET sql_mode='NO_AUTO_VALUE_ON_ZERO';` And if that doesn't work because you have multiple zeros for some reason, you could use a row-counter to explicitly set all the rows to a new number. – mpen Sep 16 '20 at 06:07
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

- 11
- 4
This is to alter the column adding PRIMARY key:
ALTER TABLE `schema_name`.`table_name`
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ,
ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
ADD PRIMARY KEY (`id`);
I copied it from MySQL Workbench... I got curious to see if it was possible to do it all in one command. I'm a little rusty in SQL.
If you are working in an specific schema, you don't need to specify it.
The above statement will create the index, set the column as the PRIMARY KEY as well with just one query.
KEEP IN MIND: There could not be duplicated values in the same column, if there are, the statement will fail to commit.

- 3,285
- 2
- 29
- 26
-
You shouldn't add both a `unique` and `primary` key to the same column. PRIMARY is already unique. – mpen Sep 23 '22 at 01:55
-
Tell that to the Workbench developers at Oracle. As I said, this is copied directly from Workbench. Is an auto generated code. But what you say make sense, I didn't pay much attention to that fact. @mpen – raphie Sep 23 '22 at 03:06
ALTER TABLE Table name ADD column datatype AUTO_INCREMENT,ADD primary key(column);

- 28,239
- 13
- 95
- 121

- 5
- 2
-
Are you sure about this? The OP asked about eight years ago(!) how to modify an **existing** column – Nico Haase Jan 08 '19 at 13:54
-
sorry but he asked about how to modify an existing column, your answer doesn't have sense! – Leo Gasparrini Apr 09 '19 at 04:03