修复 MySQL 错误 1075 (42000): 表定义错误;只能有一个自动列且必须将其定义为键


要修复此错误,需要将 PRIMARY KEY 添加到自增字段。下面让我们看看此错误如何发生的 -

在此处在创建表时出现相同的错误 -

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

要解决上述错误,需要添加 PRIMARY KEY 和 AUTO_INCREMENT。让我们先创建一个表 -

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)

更新于:2019 年 10 月 7 日

12K+ 浏览量

启动您的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.