Fix MySQL ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key


To fix this error, you need to add PRIMARY KEY to auto_increment field. Let us now see how this error occurs −

Here, we are creating a table and it gives the same error −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT,
   StudentName varchar(40),
   StudentAge int
);
ERROR 1075 (42000) : Incorrect table definition; there can be only one auto column and it must be defined as a key

To solve the above error, you need to add PRIMARY KEY with AUTO_INCREMENT. Let us first create a table −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(40),
   StudentAge int
);
Query OK, 0 rows affected (1.01 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentName,StudentAge) values('Chris Brown',19);
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('David Miller',18);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('John Doe',20);
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement :

mysql> select *from DemoTable;

This will produce the following output −

+-----------+--------------+------------+
| StudentId | StudentName  | StudentAge |
+-----------+--------------+------------+
|         1 | Chris Brown  |         19 |
|         2 | David Miller |         18 |
|         3 | John Doe     |         20 |
+-----------+--------------+------------+
3 rows in set (0.00 sec)
raja
Published on 07-Oct-2019 16:25:19
Advertisements