如何在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)
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便