如何在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 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练