Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

I have a serial no. column which is auto increment, but I want enrollment id. to be the primary key and MySQL is just not allowing me to do that. Is there any way around to do that?

like image 714
Akash Gupta Avatar asked Jul 13 '13 19:07

Akash Gupta


3 Answers

You can only define a column as AUTO_INCREMENT if it is a PRIMARY KEY and an INT (not sure of this but BIGINT will work too). Since you want the SerialNo to be set as AUTO_INCREMENT, why not make it as PRIMARY KEY and the EnrollmentID as UNIQUE?

CREATE TABLE TableName
(
    SerialNo INT AUTO_INCREMENT PRIMARY KEY,
    EnrollmentID INT UNIQUE,
    -- other columns...
)
like image 150
John Woo Avatar answered Oct 23 '22 04:10

John Woo


Make sure you define your serial number column as UNIQUE.

like image 24
Niet the Dark Absol Avatar answered Oct 23 '22 02:10

Niet the Dark Absol


CREATE TABLE tbl_login ( id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY , first_name varchar(100) NOT NULL, last_name varchar(100) NOT NULL, gender varchar(30) NOT NULL, email varchar(200) NOT NULL, password varchar(200) NOT NULL, address text NOT NULL, mobile_no varchar(15) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

like image 20
Shiv kumar K Avatar answered Oct 23 '22 03:10

Shiv kumar K