Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Error: #1075

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?

like image 810
user1888406 Avatar asked Dec 19 '12 23:12

user1888406


People also ask

How do I find MySQL errors?

On Ubuntu systems, the default location for the MySQL is /var/log/mysql/error. log . In many cases, the error logs are most easily read with the less program, a command line utility that allows you to view files but not edit them: sudo less /var/log/mysql/error.

What is 42000 error in MySQL?

The ERROR 1064 (42000) mainly occurs when the syntax isn't set correctly i.e. error in applying the backtick symbol or while creating a database without them can also create an error, if you will use hyphen in the name, for example, Demo-Table will result in ERROR 1064 (42000).


1 Answers

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

like image 88
spencer7593 Avatar answered Oct 02 '22 00:10

spencer7593