当前位置:网站首页>there can be only one auto column and it must be defined as a key

there can be only one auto column and it must be defined as a key

2022-06-23 22:11:00 User 14527

MySQL Auto increment of primary key and non primary key settings :
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

 Start :
MySQL [liangcb]> show create table a5; 
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | 
Table | Create Table    
                                                                                                                                      | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | a5    | CREATE TABLE `a5` (   `id` int(11) DEFAULT NULL,   `name` char(30) DEFAULT NULL,   `id1` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
 MySQL [liangcb]> desc a5;
  +-------+----------+------+-----+---------+-------+ | 
 Field | Type     | Null | Key | Default | Extra |
   +-------+----------+------+-----+---------+-------+ |
 id    | int(11)  | YES  || NULL    || | name  | char(30) | YES  || NULL    || | id1   | int(11)  | YES  || NULL    
 || +-------+----------+------+-----+---------+-------+ 
 3 rows in set (0.00 sec)  
 
 MySQL [liangcb]> alter table a5 CHANGE  id id int auto_increment; 
 ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key 
 // Error reporting can only have one auto increment column , And it must be defined as key
 
 MySQL [liangcb]> alter table a5 CHANGE  id id int auto_increment PRIMARY KEY ;
 Query OK, 0 rows affected (0.00 sec) Records: 0  Duplicates: 0  Warnings: 0 
 // There is no problem setting the primary key 
 
 MySQL [liangcb]> desc a5;
 +-------+----------+------+-----+---------+----------------+ | Field | Type     | Null | Key | Default | Extra          
 | +-------+----------+------+-----+---------+----------------+ | 
 id    | int(11)  | NO   | PRI | NULL    | auto_increment | | name  | char(30) | YES  || NULL    
 || | id1   | int(11)  | YES  || NULL    || 
 +-------+----------+------+-----+---------+----------------+ 
 3 rows in set (0.00 sec)
// Add auto increment to non primary key 

MySQL [liangcb]> ALTER TABLE a5
-> MODIFY COLUMN id int(11) NOT NULL FIRST ;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

MySQL [liangcb]> desc a5;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   | PRI | NULL    ||
| name  | char(30) | YES  || NULL    ||
| id1   | int(11)  | YES  || NULL    ||
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MySQL [liangcb]> alter table a5 CHANGE id1 id1 int auto_increment;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
// Error reporting must be one key

MySQL [liangcb]> alter table a5 CHANGE id1 id1 int auto_increment Unique;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

MySQL [liangcb]> desc a5;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    ||
| name  | char(30) | YES  || NULL    ||
| id1   | int(11)  | NO   | UNI | NULL    | auto_increment |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

 summary :
1、 A table can only have one auto increment field ; If the primary key is already self incremented , Then this watch can no longer have self increment .
2、ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
 Add auto increment field must be a key Whether it's Unique still PRIMARY KEY Fine .
原网站

版权声明
本文为[User 14527]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112181016065389.html