如何在mysql中重置表的主键?
重置表的主键意味着将auto_increment属性重置为1。语法如下,以重置表的主键。
alter table yourTableName auto_increment = 1;
为了理解,让我们创建一个表-
mysql> create table ResetPrimaryKey −> ( −> Id int auto_increment, −> PRIMARY KEY(Id) −> );
在表中插入一些记录。插入记录的查询如下-
mysql> insert into ResetPrimaryKey values(); mysql> insert into ResetPrimaryKey values(); mysql> insert into ResetPrimaryKey values(); mysql> insert into ResetPrimaryKey values();
现在,您可以在select语句的帮助下显示所有记录。查询如下-
mysql> select *from ResetPrimaryKey;
以下是仅显示ID(即主键)的输出:
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | +----+ 4 rows in set (0.00 sec)
这是使用alter重置表主键的查询-
mysql> alter table ResetPrimaryKey auto_increment = 1; Records: 0 Duplicates: 0 Warnings: 0
用于查询是否已成功添加auto_increment属性的查询:
mysql> desc ResetPrimaryKey;
以下是输出-
+-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | +-------+---------+------+-----+---------+----------------+ 1 row in set (0.11 sec)
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短