mysql ID auto increment doesn't starts from 0

China☆狼群 提交于 2019-12-03 11:07:23

You may either truncate the table using

TRUNCATE `table`

Truncate will delete all of the data in the table. Note that using TRUNCATE will also not fire any DELETE triggers like DELETE.

Or you can reset the count using

ALTER TABLE `table` AUTO_INCREMENT = 1

It is however the expected behavior. Don't do the latter unless the table is truly empty.

To insert a primary key with value 0, execute first the following request :

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";

Removing all the elements in a table does not affect the primary key. The primary key will still autoincrement from its last value.

If you want to reset the primary key, you can try to truncate the table:

TRUNCATE TABLE yourtable;

This clears all the elements from yourtable. Whether it resets the primary key depends on your database. With Mysql it should work, as Mysql deletes and recreates the table on TRUNCATE.

You have to use the TRUNCATE statement to restart the auto_increment from 1. It's the normal behavior to avoid braking things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!