238

I’m trying to modify a table to make its primary key column AUTO_INCREMENT after the fact. I have tried the following SQL, but got a syntax error notification.

ALTER TABLE document
ALTER COLUMN document_id AUTO_INCREMENT

Am I doing something wrong or is this not possible?

+--------------------+
| VERSION()          |
+--------------------+
| 5.0.75-0ubuntu10.2 |
+--------------------+
TRiG
  • 10,148
  • 7
  • 57
  • 107
C. Ross
  • 31,137
  • 42
  • 147
  • 238

21 Answers21

455
ALTER TABLE document MODIFY COLUMN document_id INT auto_increment
roman
  • 11,143
  • 1
  • 31
  • 42
  • 3
    I'm curious why you would suggest INT(4). Any particular reason? – Steven Oxley Jan 30 '10 at 19:34
  • 3
    @Steven Oxley because I declared my table that way. – C. Ross Jan 30 '10 at 19:48
  • 34
    Specifying a number between parenthesis does exactly *nothing* (well ok, almost nothing) for MySQL integer types. The only thing that may be influenced by the number is the display width, and it is up to the client to do that. But don't be deceived and think that it works like it does for VARCHAR and DECIMAL types - in those cases, the amount of data you can store in there is actually specified, whereas a particular flavour of INT is always allows storage of the exact same range of values – Roland Bouman Jan 30 '10 at 20:03
  • I always get an primary key error while doing this operations, so I have this is what I do: SET FOREIGN_KEY_CHECKS = 0; ALTER TABLE document DROP PRIMARY KEY; ALTER TABLE document MODIFY COLUMN document_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL; SET FOREIGN_KEY_CHECKS = 1; – Fernando Jul 13 '19 at 15:50
  • This doesn't seem to work in MySQL because you need to set the column as a primary key since only one column is allowed to be auto_increment (also see [Roland Bouman's answer below](https://stackoverflow.com/a/2169099/1440546) – charlchad Sep 15 '19 at 04:51
91

Roman is right, but note that the auto_increment column must be part of the PRIMARY KEY or a UNIQUE KEY (and in almost 100% of the cases, it should be the only column that makes up the PRIMARY KEY):

ALTER TABLE document MODIFY document_id INT AUTO_INCREMENT PRIMARY KEY
pawstrong
  • 924
  • 7
  • 17
Roland Bouman
  • 31,125
  • 6
  • 66
  • 67
13

In my case it only worked when I put not null. I think this is a constraint.

ALTER TABLE document MODIFY COLUMN document_id INT NOT NULL AUTO_INCREMENT;
TRiG
  • 10,148
  • 7
  • 57
  • 107
Rabia Naz khan
  • 507
  • 4
  • 13
12

You can apply the atuto_increment constraint to the data column by the following query:

ALTER TABLE customers MODIFY COLUMN customer_id BIGINT NOT NULL AUTO_INCREMENT;

But, if the columns are part of a foreign key constraint you, will most probably receive an error. Therefore, it is advised to turn off foreign_key_checks by using the following query:

SET foreign_key_checks = 0;

Therefore, use the following query instead:

SET foreign_key_checks = 0;
ALTER TABLE customers MODIFY COLUMN customer_id BIGINT NOT NULL AUTO_INCREMENT;
SET foreign_key_checks = 1;
Meisam Rasouli
  • 301
  • 2
  • 5
  • This should be the accepted answer. In real-life situations the PK is very likely to be part of a FK constraint. – E-Riz Aug 16 '23 at 13:08
8

You must specify the type of the column before the auto_increment directive, i.e. ALTER TABLE document MODIFY COLUMN document_id INT AUTO_INCREMENT.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Håvard S
  • 23,244
  • 8
  • 61
  • 72
8

The SQL to do this would be:

ALTER TABLE `document` MODIFY COLUMN `document_id` INT AUTO_INCREMENT;

There are a couple of reasons that your SQL might not work. First, you must re-specify the data type (INT in this case). Also, the column you are trying to alter must be indexed (it does not have to be the primary key, but usually that is what you would want). Furthermore, there can only be one AUTO_INCREMENT column for each table. So, you may wish to run the following SQL (if your column is not indexed):

ALTER TABLE `document` MODIFY `document_id` INT AUTO_INCREMENT PRIMARY KEY;

You can find more information in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html for the modify column syntax and http://dev.mysql.com/doc/refman/5.1/en/create-table.html for more information about specifying columns.

Steven Oxley
  • 6,563
  • 6
  • 43
  • 55
7

AUTO_INCREMENT is part of the column's datatype, you have to define the complete datatype for the column again:

ALTER TABLE document
ALTER COLUMN document_id int AUTO_INCREMENT

(int taken as an example, you should set it to the type the column had before)

Dan Soap
  • 10,114
  • 1
  • 40
  • 49
6

You can do it like this:

 alter table [table_name] modify column [column_name] [column_type] AUTO_INCREMENT;
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Aditya Kumar
  • 61
  • 1
  • 1
5

Below statement works. Note that you need to mention the data type again for the column name (redeclare the data type the column was before).

ALTER TABLE document
MODIFY COLUMN document_id int AUTO_INCREMENT;
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
user5082888
  • 51
  • 1
  • 1
5

You can use the following query to make document_id to increment automatically

ALTER TABLE document MODIFY COLUMN document_id INT auto_increment

It is preferred to make document_id primary key as well

ALTER TABLE document MODIFY COLUMN document_id INT auto_increment PRIMARY KEY;
Azhar Zafar
  • 1,554
  • 1
  • 10
  • 13
  • 1
    Note that if that column is already a primary key you can't declare it again there else it gives error about multiple primary keys (you'd expect it to be a bit more clever there and see that it's the same primary key I guess). So in that case you just skip the PRIMARY KEY and it doesn't affect the existing primary key setting on that column – George Birbilis May 18 '18 at 02:08
4

Setting column as primary key and auto_increment at the same time:

  mysql> ALTER TABLE persons MODIFY COLUMN personID INT auto_increment PRIMARY KEY;
    Query OK, 10 rows affected (0.77 sec)
    Records: 10  Duplicates: 0  Warnings: 0

    mysql>
3

AUTO_INCREMENT is part of the column's datatype, you have to define the complete datatype for the column again:

ALTER TABLE document
MODIFY COLUMN document_id int AUTO_INCREMENT

(int taken as an example, you should set it to the type the column had before)

fedorqui
  • 275,237
  • 103
  • 548
  • 598
KKK
  • 31
  • 1
3

If none of the above works try this. This is what I did in MYSQL and yes, you need to write the column name (document_id) twice.

ALTER TABLE document
CHANGE COLUMN document_id document_id INT(11) NOT NULL AUTO_INCREMENT ;
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Susie
  • 5,038
  • 10
  • 53
  • 74
2

To modify the column in mysql we use alter and modify keywords. Suppose we have created a table like:

create table emp(
    id varchar(20),
    ename varchar(20),
    salary float
);

Now we want to modify type of the column id to integer with auto increment. You could do this with a command like:

alter table emp modify column id int(10) auto_increment;
solarissmoke
  • 30,039
  • 14
  • 71
  • 73
2
alter table tbl_user MODIFY COLUMN id int(10) auto_increment;
Pang
  • 9,564
  • 146
  • 81
  • 122
Aminur Rahman
  • 400
  • 1
  • 6
  • 14
2

Previous Table syntax:

CREATE TABLE apim_log_request (TransactionId varchar(50) DEFAULT NULL);

For changing the TransactionId to auto increment use this query

ALTER TABLE apim_log_request MODIFY COLUMN TransactionId INT auto_increment;
Rob
  • 26,989
  • 16
  • 82
  • 98
Usman Yaqoob
  • 535
  • 5
  • 13
2

Just like this:

alter table document modify column id int(11) auto_increment;

apemost
  • 41
  • 4
1

As you are redefining the column again, you have to specify the datatype again and add auto_increment to it as it's a part of datatype.

ALTER TABLE `document` MODIFY COLUMN `document_id` INT AUTO_INCREMENT;
Optimizer
  • 687
  • 7
  • 7
1

Try the following:

ALTER TABLE table_name MODIFY COLUMN id datatype auto_increment;
xKobalt
  • 1,498
  • 2
  • 13
  • 19
-1

Since SQL tag is attached to the question I really think all answers missed one major point.

MODIFY command does not exist in SQL server So you will be getting an error when you run the

ALTER TABLE Satellites MODIFY COLUMN SatelliteID INT auto_increment PRIMARY KEY;

enter image description here

In this case you can either add new column as INT IDENTITY

ALTER TABLE Satellites
   ADD ID INT IDENTITY
       CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED;

OR


Fill the existing null index with incremental numbers using this method,
DECLARE @id INT
SET @id = 0 
UPDATE Satellites SET @id = SatelliteID = @id + 1 
INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
-3

Use the following queries:

ALTER TABLE YourTable DROP COLUMN IDCol

ALTER TABLE YourTable ADD IDCol INT IDENTITY(1,1)
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64