9

SQL query:

ALTER TABLE  `blog` CHANGE  `id`  `id` BIGINT NOT NULL AUTO_INCREMENT

MySQL said:

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

I am trying to create a blog, and I got the code done. Now, I need to make the id auto increase, but I get this error. Why am I getting this?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
user1888406
  • 97
  • 1
  • 1
  • 4

3 Answers3

11

MySQL is returning that error (most likely) because there is no unique index defined on the id column. (MySQL requires that there be a unique index. The other possibility, which you would have already figured out, is that there can be only one column defined as AUTO_INCREMENT within the table.)

To get that column to be an AUTO_INCREMENT, you can add either a UNIQUE constraint or a PRIMARY KEY constraint on the id column. For example:

ALTER TABLE `blog` ADD CONSTRAINT `blog_ux` UNIQUE (`id`) ;

(Note that this statement will return an error if any duplicate values exist for the id column.)

Alternatively, you can make the id column the PRIMARY KEY of the table (if the table doesn't already have a PRIMARY KEY constraint defined).

ALTER TABLE `blog` ADD PRIMARY KEY (`id`) ;

(Note that this statement will return an error if any duplicate value exist for the id column, OR if there are any NULL values stored in that column, of if there is already a PRIMARY KEY constraint defined on the table.)

spencer7593
  • 106,611
  • 15
  • 112
  • 140
7

MySQL requires you to make auto increment column the primary key of a table. Add the primary key constraint at the end

ALTER TABLE  `blog` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT primary key
codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • 2
    Just to clarify, MySQL does not require that a column be the primary key for a table in order to make it an AUTO_INCREMENT column. What MySQL requires is that there be a UNIQUE constraint on the column. A column that is the PRIMARY KEY column is eligible to be AUTO_INCREMENT; but it's not, strictly speaking, a requirement. – spencer7593 Dec 19 '12 at 23:52
1

To resolve #1075 error message you need to mark atleast one column as primary_key or unique_key. that you have forgot to do.

By defining Primary _key on ID column my error is resolved guys.

thanks

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Anirudh Sood
  • 1,418
  • 12
  • 6