修復 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.