php 重置 ID 自动递增 ? phpmyadmin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3019698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset ID autoincrement ? phpmyadmin
提问by Marcelo
I was testing some data in my tables of my database, to see if there was any error, now I cleaned all the testing data, but my id (auto increment) does not start from 1 anymore, can (how do) I reset it ?
我正在测试我的数据库表中的一些数据,看看是否有任何错误,现在我清理了所有测试数据,但是我的 id(自动增量)不再从 1 开始,我可以(如何)重置它?
回答by Sev
ALTER TABLE `table_name` AUTO_INCREMENT=1
回答by Bryan Downing
You can also do this in phpMyAdmin without writing SQL.
您也可以在 phpMyAdmin 中执行此操作,而无需编写 SQL。
- Click on a database name in the left column.
- Click on a table name in the left column.
- Click the "Operations" tab at the top.
- Under "Table options" there should be a field for AUTO_INCREMENT (only on tables that have an auto-increment field).
- Input desired value and click the "Go" button below.
- 单击左列中的数据库名称。
- 单击左列中的表名称。
- 单击顶部的“操作”选项卡。
- 在“表选项”下应该有一个 AUTO_INCREMENT 字段(仅在具有自动增量字段的表上)。
- 输入所需的值,然后单击下面的“Go”按钮。
Note:You'll see that phpMyAdmin is issuing the same SQL that is mentioned in the other answers.
注意:您会看到 phpMyAdmin 发出了与其他答案中提到的相同的 SQL。
回答by Chen
ALTER TABLE xxxAUTO_INCREMENT =1;
or
clear your table by TRUNCATE
ALTER TABLE xxxAUTO_INCREMENT =1; 或通过 TRUNCATE 清除您的表格
回答by Mikeys4u
I agree with rpd, this is the answer and can be done on a regular basis to clean up your id column that is getting bigger with only a few hundred rows of data, but maybe an id of 34444543!, as the data is deleted out regularly but id is incremented automatically.
我同意 rpd,这是答案,可以定期清理您的 id 列,该列只有几百行数据,但可能是 34444543!,因为数据被删除了定期但 id 会自动增加。
ALTER TABLE users DROP id
The above sql can be run via sql query or as php. This will delete the id column.
上述 sql 可以通过 sql 查询或 php 运行。这将删除 id 列。
Then re add it again, via the code below:
然后重新添加它,通过下面的代码:
ALTER TABLE `users` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
Place this in a piece of code that may get run maybe in an admin panel, so when anyone enters that page it will run this script that auto cleans your database, and tidys it.
把它放在一段可能会在管理面板中运行的代码中,这样当任何人进入该页面时,它都会运行这个脚本来自动清理你的数据库,并整理它。
回答by rpd
I have just experienced this issue in one of my MySQL db's and I looked at the phpMyAdmin answer here. However the best way I fixed it in phpMyAdmin was in the affected table, drop the id column and make a fresh/new id column (adding A-I -autoincrement-). This restored my table id correctly-simples! Hope that helps (no MySQL code needed-I hope to learn to use that but later!) anyone else with this problem.
我刚刚在我的 MySQL 数据库之一中遇到了这个问题,我在这里查看了 phpMyAdmin 的答案。然而,我在 phpMyAdmin 中修复它的最好方法是在受影响的表中,删除 id 列并创建一个新的/新的 id 列(添加 AI -autoincrement-)。这正确地恢复了我的表 id - 简单!希望有帮助(不需要 MySQL 代码 - 我希望以后能学会使用它!)其他有这个问题的人。

