92

I created a table in MySQL with on column itemID.

After creating the table, now I want to change this column to AUTOINCREMENT.

How can this be done using ALTER statements?

Table definition:

ALLITEMS (itemid int(10) unsigned, itemname varchar(50))

I am using the following code but it is throwing a syntax error

Error: syntax incorrect.

ALTER TABLE allitems
MODIFY itemid INT(10) UNSIGNED AUTOINCREMENT; 
2
  • 1
    Duplicate of: stackoverflow.com/questions/4795382/… Commented Jul 17, 2014 at 6:44
  • 7
    AUTOINCREMENT has an underscore: AUTO_INCREMENT.... Commented Feb 23, 2015 at 10:56

9 Answers 9

133
CREATE TABLE ALLITEMS(
    itemid INT(10)UNSIGNED,
    itemname VARCHAR(50)
);

ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT PRIMARY KEY;

DESC ALLITEMS;

INSERT INTO ALLITEMS(itemname)
VALUES
    ('Apple'),
    ('Orange'),
    ('Banana');

SELECT
    *
FROM
    ALLITEMS;

I was confused with CHANGE and MODIFY keywords before too:

ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT PRIMARY KEY;

ALTER TABLE ALLITEMS MODIFY itemid INT(5);

While we are there, also note that AUTO_INCREMENT can also start with a predefined number:

ALTER TABLE tbl AUTO_INCREMENT = 100;
Sign up to request clarification or add additional context in comments.

5 Comments

I already defined identity in SQL server 2012 but i am trying to pass some value to db. So at that place( auto-incremented) what do I need to pass in VS 2010??
ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT . You don't need to specify "PRIMARY KEY". It may result in error: "Multiple primary key defined". Besides that, great answer!
@ThinkCode When to use CHANGE and MODIFY COLUMN?
-1; by not respecifying UNSIGNED in the CHANGE clause, you turn itemid into a SIGNED int and thus reduce its maximum value.
To add on @lepe's comment: it's not that it may, but it always produces "Multiple primary key defined" error for me. Removing "PRIMARY KEY" was necessary in my case.
20

The syntax:

   ALTER TABLE `table1` CHANGE `itemId` `itemId` INT( 11 ) NOT NULL AUTO_INCREMENT 

But the table needs a defined key (ex primary key on itemId).

1 Comment

what's the distinction between ex primary key and primary key on itemid?
17
ALTER TABLE `ALLITEMS`
    CHANGE COLUMN `itemid` `itemid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

1 Comment

This should be the accepted answer. Very rarely will anyone have the need to use negative (signed) id columns.
13

Basic syntax for adding an AUTO_INCREMENT PRIMARY KEY to the OP's existing table:

ALTER TABLE allitems
MODIFY itemid INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY;

Or for a new table, here's the syntax example from the docs:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

Traps and things to note:

  • An AUTO_INCREMENT column must have an index on it. (Usually, you'll want it to be the PRIMARY KEY, but MySQL does not require this.)
  • It's usually a good idea to make your AUTO_INCREMENT columns UNSIGNED. From the docs:

    Use the UNSIGNED attribute if possible to allow a greater range.

  • When using a CHANGE or MODIFY clause to make a column AUTO_INCREMENT (or indeed whenever you use a CHANGE or MODIFY clause) you should be careful to include all modifiers for the column, like NOT NULL or UNSIGNED, that show up in the table definition when you call SHOW CREATE TABLE yourtable. These modifiers will be lost otherwise.

Comments

5
ALTER TABLE allitems
CHANGE itemid itemid INT(10) AUTO_INCREMENT;

Comments

1
ALTER TABLE tblcatalog
    CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT FIRST;

Comments

1

ALTER TABLE employees CHANGE id id int AUTO_INCREMENT PRIMARY KEY;

Comments

1

To reset the value of auto increment:

ALTER TABLE [table_name] AUTO_INCREMENT = start_value;

Comments

-1
ALTER TABLE t_name modify c_name INT(10) AUTO_INCREMENT PRIMARY KEY;

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.