如何将MySQL中的主键更改为auto_increment?
要将主键更改为auto_increment,可以使用MODIFY命令。让我们首先创建一个表。
mysql> create table changePrimaryKeyInAutoIncrement -> ( -> StudentId int not null primary key, -> StudentName varchar(100), -> StudentAge int, -> StudentAddress varchar(100) -> );
现在让我们使用desc命令检查表的描述:
mysql> desc changePrimaryKeyInAutoIncrement;
这将产生以下输出
+----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | StudentId | int(11) | NO | PRI | NULL | | | StudentName | varchar(100) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentAddress | varchar(100) | YES | | NULL | | +----------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
看上面的示例输出,StudentId列是主键。现在让我们将主键更改为auto_increment:
mysql> alter table changePrimaryKeyInAutoIncrement MODIFY StudentId INT AUTO_INCREMENT; Records: 0 Duplicates: 0 Warnings: 0
让我们再次检查表的描述:
mysql> desc changePrimaryKeyInAutoIncrement;
这将产生以下输出
+----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(100) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentAddress | varchar(100) | YES | | NULL | | +----------------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
查看上面的示例输出,StudentId列已更改为auto_increment。
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短