在表中有数据时重置AUTO_INCREMENT似乎没有效果

在表中有数据时重置AUTO_INCREMENT似乎没有效果

控制台输出如下:


mysql> SELECT * FROM user;
+-----+----------+
| id  | username |
+-----+----------+
| 119 | Andy     |
| 118 | Linus    |
| 117 | Mike     |
+-----+----------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE user
    -> AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE user;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=120 DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> ALTER TABLE user AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE user;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=120 DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> INSERT user(username) VALUES("John");
Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM user;
+-----+----------+
| id  | username |
+-----+----------+
| 119 | Andy     |
| 120 | John     |
| 118 | Linus    |
| 117 | Mike     |
+-----+----------+
4 rows in set (0.00 sec)

mysql> DELETE FROM user;
Query OK, 4 rows affected (0.01 sec)

mysql> ALTER TABLE user AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE user;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                              |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> INSERT USER(username) VALUES("Mike"),("Linus"),("Andy");
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE user;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                               |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> DELETE FROM user;
Query OK, 3 rows affected (0.01 sec)

mysql> SHOW CREATE TABLE user;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                               |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,经测试运行sql是可以正确重置的,如下:

http://img1.sycdn.imooc.com//climg/5fc5b641097dea2406100338.jpg

http://img1.sycdn.imooc.com//climg/5fc5b661091feff205800380.jpg

http://img1.sycdn.imooc.com//climg/5fc5b67e093e5e9906250393.jpg

http://img1.sycdn.imooc.com//climg/5fc5b69109440cdd05940264.jpg

建议同学重新执行sql试试

祝学习愉快~

  • Wonwayshon 提问者 #1
    老师我试的情况是先在表中存一些记录并在ALTER user AUTO_INCREMENT=0;之前不使用DELETE FROM user;清空记录,操作完成之后用SHOW CREATE TABLE user;检查看到没有重置。 只有在DELETE FROM user;之后ALTER user AUTO_INCREMENT=0;检查才有重置效果
    2020-12-01 11:39:33
  • 好帮手慕小脸 回复 提问者 Wonwayshon #2

    同学你好,ALTER TABLE user AUTO_INCREMENT=1;是设置id字段重新从1开始。 id字段是自增长的,所以同学需要将该表中的数据全部删除,再运行重置的sql

    祝学习愉快~

    2020-12-01 13:34:53
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师