如何在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)
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短