Add Auto-Increment ID to existing table?

Charles Jenkins picture Charles Jenkins · Feb 7, 2013 · Viewed 279.9k times · Source

I have a pre-existing table, containing 'fname', 'lname', 'email', 'password' and 'ip'. But now I want an auto-increment column. However, when I enter:

ALTER TABLE users
ADD id int NOT NULL AUTO_INCREMENT

I get the following:

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

Any advice?:)

Answer

Muhammad Asif Mahmood picture Muhammad Asif Mahmood · Feb 7, 2013

Try this

ALTER TABLE `users` ADD `id` INT NOT NULL AUTO_INCREMENT;

for an existing primary key