After adding a new identifier column (do not set as primary key yet and do not enable auto-increment):
SET @index = 1; UPDATE tablename SET ID = (@index: =@index +1);
This sets the value of the incremental identifier, starting from 1 on each of your existing rows, thereby eliminating the duplicate key problem you would encounter if you tried to insert a new primary key column after the data has already been entered.
Once this is done, you can set the identifier column as a primary key with auto-increment.
source share