+----+--------------+-----+----------------------+----------+
| id | name         | age | email                | isActive |
+----+--------------+-----+----------------------+----------+
|  1 | 王老师       |  32 | wangwc.@163.com      |        1 |
|  2 | 王老师       |  32 | wangwc.@163.com      |        1 |
|  3 | 隔壁老王     |  32 | laowang.gebi@163.com |        1 |
|  4 | 隔壁老王     |  32 | laowang.gebi@163.com |        1 |
|  5 | 超哥哥       |  32 | brother_chao@163.com |        0 |
+----+--------------+-----+----------------------+----------+

对于上述的mysql表格中有的数据出现重复,需要删除重新添加数据,

delete from index_author where id=2;
delete from index_author where id=3;

删除之后的mysql的内容为

+----+--------------+-----+----------------------+----------+
| id | name         | age | email                | isActive |
+----+--------------+-----+----------------------+----------+
|  1 | 王老师       |  32 | wangwc.@163.com      |        1 |
|  3 | 隔壁老王     |  32 | laowang.gebi@163.com |        1 |
|  4 | 隔壁老王     |  32 | laowang.gebi@163.com |        1 |
|  5 | 超哥哥       |  32 | brother_chao@163.com |        0 |
+----+--------------+-----+----------------------+----------+

**发现数据的id值没有发生变化,于是删除后三条数据,重新添加,结果如下:**
+----+--------------+-----+----------------------+----------+
| id | name         | age | email                | isActive |
+----+--------------+-----+----------------------+----------+
|  1 | 王老师       |  32 | wangwc.@163.com      |        1 |
|  6 | 隔壁老王     |  32 | laowang.gebi@163.com |        1 |
|  7 | 超哥哥       |  32 | brother_chao@163.com |        0 |
+----+--------------+-----+----------------------+----------+


发现id值是从6开始增加。


怎么解决这个问题?

删除数据后在终端执行如下命令:

alter table 表名 AUTO_INCREMENT=n;

其中n代表是从哪个数字开始自增,大写的部分一定要大写。
示例:

alter table index_author AUTO_INCREMENT=2;

重新添加后两天数据,结果如下:

   	+----+--------------+-----+----------------------+----------+
    | id | name         | age | email                | isActive |
    +----+--------------+-----+----------------------+----------+
    |  1  | 王老师       |  32 | wangwc.@163.com      |        1 |
    |  2  | 隔壁老王     |  32 | laowang.gebi@163.com |        1 |
    |  3  | 超哥哥       |  32 | brother_chao@163.com |        0 |
    +-----+--------------+-----+----------------------+----------+
Logo

更多推荐