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

mysqlmysqli database

要修复此错误,您需要将 PRIMARY KEY 添加到 auto_increment 字段。现在让我们看看此错误是如何发生的 −

在这里,我们正在创建一个表,它给出了相同的错误 −

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

要解决上述错误,您需要添加带有 AUTO_INCREMENT 的 PRIMARY KEY。让我们首先创建一个表 −

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 命令在表中插入一些记录 −

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)

使用 select 语句显示表中的所有记录:

mysql> select *from DemoTable;

这将产生以下输出 −

+-----------+--------------+------------+
| StudentId | StudentName  | StudentAge |
+-----------+--------------+------------+
|         1 | Chris Brown  |         19 |
|         2 | David Miller |         18 |
|         3 | John Doe     |         20 |
+-----------+--------------+------------+
3 rows in set (0.00 sec)

相关文章