Add Auto-Increment ID to existing table?
Asked Answered
K

18

118

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

Kelly answered 7/2, 2013 at 14:20 Comment(5)
can you post your existing table definition?Woolcott
please post output of describe usersOctad
tried that, but it just returns the table. what do you need?Kelly
@CharlesJenkins Could you pick the correct answer?Shrier
MODIFY can also be used as : ALTER TABLE users MODIFY id int NOT NULL AUTO_INCREMENT ; /* AUG Dated 2019*/Shevat
H
151

Try this

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

for an existing primary key

Horrocks answered 7/2, 2013 at 14:31 Comment(6)
#1068 - Multiple primary key definedKelly
In SQL Server 2008, replacing 'AUTO_INCREMENT' with 'IDENTITY(1,1)' worked for me.Cornew
Note that this adds an ID-column as the last column in the table. Add FIRST to the query to make it the first in this table.Grolier
@CharlesJenkins To avoid the "Multiple primary key" error, drop the existing primary key before defining the new one: ALTER TABLE users DROP PRIMARY KEY, ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEYGuiltless
To make it the first column, use FIRST e.g ALTER TABLE tbl1 ADD id INT PRIMARY KEY AUTO_INCREMENT FIRSTLocution
Got errors with that sintax this worked for me ALTER TABLE users ADD id INT NOT NULL AUTO_INCREMENT , ADD PRIMARY KEY (id);Marden
O
116

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.

Orth answered 27/6, 2013 at 15:0 Comment(6)
This worked for me and set the auto_increment id column as the primary key.Duralumin
What does the FIRST do there?Marginalia
@Cees Timmerman: The keyword FIRST indicates that the new column will be the first column in the table. Alternatively, you can specify a particular position using AFTER existent_col_name. If neither FIRST nor AFTER are used then the new column is added after all current columns.Lipolysis
what if I want to create an index on id but not a PK?Fungible
@Fungible this adds a UNIQUE index which is an index in itself. I don't like having a column called id which isn't a PK though.Auxesis
UNIQUE did the trick for me. Struggled for a while altering an table with existing primary keys but needed auto_increment. Thanks :)Manganate
H
47

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
Haffner answered 7/2, 2013 at 14:24 Comment(3)
that didn't work! Got this error: #1068 - Multiple primary key definedKelly
you get Multiple primary key defined because you already have a primary key on the table. A table can only have one primary key. Retry the above command on the table when it doesn't have a primary keyMistranslate
To avoid the "Multiple primary key" error, drop the existing primary key before defining the new one: ALTER TABLE users DROP PRIMARY KEY, ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEYGuiltless
M
28

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
Mendelssohn answered 30/1, 2014 at 15:50 Comment(2)
cannot drop, then ADD id. Will get Duplicate column name 'id'Hafnium
@JohnJoe, that's because you already have the id column. Try this: ALTER TABLE nametable DROP PRIMARY KEY, MODIFY COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEYWadewadell
C
20

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;
Corydalis answered 7/2, 2013 at 14:27 Comment(7)
got this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LTER TABLE users ADD id int NOT NULL AUTO_INCREMENT PRIMARY KEY(id)' at line 1Kelly
look your code u posted it miss LTER and not ALter , make sure u copied it allCorydalis
thanks, but now i've got this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE users ADD id int NOT NULL AUTO_INCREMENT PRIMARY KEY(id)' at line 3 sorry!Kelly
got this: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(id)' at line 1Kelly
well you can follow those steps in my answerCorydalis
You had 3 typos in this answer (modify not change, "id id" and comments need a space after them). I have edited the answer to fix them.Inverness
mysql> ALTER TABLE domains modify COLUMN id INT AUTO_INCREMENT UNIQUE FIRST; Query OK, 2 rows affected (0.27 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> desc domains; +--------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | UNI | NULL | auto_increment | | domain | varchar(128) | NO | PRI | NULL | | +--------+--------------+------+-----+---------+----------------+Lysippus
A
18

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....!

Adscititious answered 12/3, 2019 at 4:49 Comment(0)
T
11

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;
Twice answered 22/5, 2018 at 12:58 Comment(0)
A
7

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.

Acidophil answered 27/4, 2021 at 14:44 Comment(2)
It's also possible to perform the last 2 steps in a single query, though: ALTER TABLE PremiumPackages ADD COLUMN id MEDIUMINT(8) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;Doctrinaire
it did not workStealing
H
6

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;
Hafnium answered 22/3, 2017 at 2:27 Comment(0)
B
4
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

Bremer answered 22/6, 2013 at 18:46 Comment(0)
Y
3
ALTER TABLE users ADD id int NOT NULL AUTO_INCREMENT primary key FIRST
Yolandayolande answered 27/1, 2014 at 4:40 Comment(0)
M
2

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); 
Mumford answered 1/7, 2022 at 18:37 Comment(0)
G
1

For PostgreSQL you have to use SERIAL instead of auto_increment.

ALTER TABLE your_table_name ADD COLUMN id SERIAL NOT NULL PRIMARY KEY
Germanophobe answered 16/12, 2019 at 17:20 Comment(0)
L
1

This SQL request works for me :

ALTER TABLE users
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ;
Luby answered 15/1, 2021 at 16:26 Comment(0)
N
0
ALTER TABLE `table` ADD `id` INT NOT NULL AUTO_INCREMENT unique

Try this. No need to drop your primary key.

Nashville answered 12/11, 2018 at 13:43 Comment(0)
T
0

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; ;

Terminator answered 3/2 at 21:6 Comment(0)
S
-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.

Sauncho answered 13/12, 2013 at 17:2 Comment(0)
D
-2

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.

Dissolution answered 25/8, 2014 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.