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

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

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

在这里,我们创建了一个表,并给出了相同的错误 –

mysql> create table DemoTable
(
    StudentId int NOT NULL AUTO_INCREMENT,
    StudentName varchar(40),
    StudentAge int
);
ERROR 1075(42000):错误的表定义;只能有一个自动列,必须将其定义为键

要解决上述错误,您需要使用AUTO_INCREMENT添加PRIMARY KEY。让我们首先创建一张表 –

mysql> create table DemoTable
(
    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    StudentName varchar(40),
    StudentAge int
);
Query OK,0行受影响(1.01秒)

使用insert命令在表中插入一些记录 –

mysql> insert into DemoTable(StudentName,StudentAge) values('Chris Brown',19);
Query OK,1行受影响(0.30秒)
mysql> insert into DemoTable(StudentName,StudentAge) values('David Miller',18);
Query OK,1行受影响(0.20秒)
mysql> insert into DemoTable(StudentName,StudentAge) values('John Doe',20);
Query OK,1行受影响(0.11秒)

使用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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程