Charles Jenkins
Charles Jenkins

Reputation: 1219

Add Auto-Increment ID to existing table?

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?:)

Upvotes: 121

Views: 424762

Answers (18)

Deepak Gautam
Deepak Gautam

Reputation: 85

ALTER TABLE yourDbName.YourTableName CHANGE COLUMN ID ID INT NOT NULL AUTO_INCREMENT , ADD PRIMARY KEY (ID), ADD UNIQUE INDEX ID_UNIQUE (ID ASC) VISIBLE; ;

Upvotes: 0

live-love
live-love

Reputation: 52366

If you want to add an id with a primary key and identity:

ALTER TABLE user ADD id INT NOT NULL AUTO_INCREMENT FIRST , ADD PRIMARY KEY (id); 

Upvotes: 2

user96279
user96279

Reputation: 111

Drop the primary index from the table:

ALTER TABLE `tableName` DROP INDEX `PRIMARY`;

Then add the id column (without a primary index). I have used a big int because I am going to have lots of data but INT(11) should work just as well:

ALTER TABLE `tableName` ADD COLUMN `id` BIGINT(11) NOT NULL FIRST;

Then modify the column with auto-increment (thanks php). It needs to be a primary key:

ALTER TABLE `tableName ` MODIFY COLUMN `id` BIGINT(11) UNSIGNED PRIMARY KEY AUTO_INCREMENT;

I have just tried this on a table of mine and it appears to have worked.

Upvotes: 8

CoulRaoul75
CoulRaoul75

Reputation: 23

This SQL request works for me :

ALTER TABLE users
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ;

Upvotes: 1

Muhammad Asif Mahmood
Muhammad Asif Mahmood

Reputation: 1710

Try this

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

for an existing primary key

Upvotes: 154

PauloBorba
PauloBorba

Reputation: 186

For PostgreSQL you have to use SERIAL instead of auto_increment.

ALTER TABLE your_table_name ADD COLUMN id SERIAL NOT NULL PRIMARY KEY

Upvotes: 1

Rajat Dabade
Rajat Dabade

Reputation: 314

If you run the following command :

ALTER TABLE users ADD id int NOT NULL AUTO_INCREMENT PRIMARY KEY;

This will show you the error :

ERROR 1060 (42S21): Duplicate column name 'id'

This is because this command will try to add the new column named id to the existing table.

To modify the existing column you have to use the following command :

ALTER TABLE users MODIFY id int NOT NULL AUTO_INCREMENT PRIMARY KEY;

This should work for changing the existing column constraint....!

Upvotes: 18

刘斌文
刘斌文

Reputation: 1

ALTER TABLE `table` ADD `id` INT NOT NULL AUTO_INCREMENT unique

Try this. No need to drop your primary key.

Upvotes: 0

Sunny Verma
Sunny Verma

Reputation: 111

Delete the primary key of a table if it exists:

 ALTER TABLE `tableName` DROP PRIMARY KEY;

Adding an auto-increment column to a table :

ALTER TABLE `tableName` ADD `Column_name` INT PRIMARY KEY AUTO_INCREMENT;

Modify the column which we want to consider as the primary key:

alter table `tableName` modify column `Column_name` INT NOT NULL AUTO_INCREMENT PRIMARY KEY;

Upvotes: 11

Bhaskar Bhatt
Bhaskar Bhatt

Reputation: 1467

If you want to add AUTO_INCREMENT in an existing table, need to run following SQL command:

 ALTER TABLE users ADD id int NOT NULL AUTO_INCREMENT primary key

Upvotes: 50

John Joe
John Joe

Reputation: 12803

Just change the ADD to MODIFY and it will works !

Replace

ALTER TABLE users ADD id int NOT NULL AUTO_INCREMENT

To

ALTER TABLE users MODIFY id int NOT NULL AUTO_INCREMENT;

Upvotes: 6

Odhik Susanto
Odhik Susanto

Reputation: 227

ALTER TABLE users CHANGE id int( 30 ) NOT NULL AUTO_INCREMENT

the integer parameter is based on my default sql setting have a nice day

Upvotes: 4

Juhan
Juhan

Reputation: 1291

Proceed like that :

Make a dump of your database first

Remove the primary key like that

ALTER TABLE yourtable DROP PRIMARY KEY

Add the new column like that

ALTER TABLE yourtable add column Id INT NOT NULL AUTO_INCREMENT FIRST, ADD primary KEY Id(Id)

The table will be looked and the AutoInc updated.

Upvotes: -2

jrltt
jrltt

Reputation: 681

First you have to remove the primary key of the table

ALTER TABLE nametable DROP PRIMARY KEY

and now yo can add the autoincrement ...

ALTER TABLE nametable ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY

Upvotes: 29

php
php

Reputation: 4425

ALTER TABLE users ADD id int NOT NULL AUTO_INCREMENT primary key FIRST

Upvotes: 3

user3100184
user3100184

Reputation: 1

Check for already existing primary key with different column. If yes, drop the primary key using:

ALTER TABLE Table1
DROP CONSTRAINT PK_Table1_Col1
GO

and then write your query as it is.

Upvotes: -1

echo_Me
echo_Me

Reputation: 37233

Well, you must first drop the auto_increment and primary key you have and then add yours, as follows:

-- drop auto_increment capability
alter table `users` modify column id INT NOT NULL;
-- in one line, drop primary key and rebuild one
alter table `users` drop primary key, add primary key(id);
-- re add the auto_increment capability, last value is remembered
alter table `users` modify column id INT NOT NULL AUTO_INCREMENT;

Upvotes: 20

Coderer
Coderer

Reputation: 27264

If you don't care whether the auto-id is used as PRIMARY KEY, you can just do

ALTER TABLE `myTable` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST;

I just did this and it worked a treat.

Upvotes: 120

Related Questions