如何重置 mysql table 中 AUTO_INCREMENT 行的值

how to reset the value of a AUTO_INCREMENT row in a mysql table

假设我有一个包含 200 条记录的 table,当我尝试向该 table 添加另一条记录时,我现在删除了所有记录,我希望记录的 ID 从1 而不是 201。 我试过这段代码但不起作用

$connection->query("ALTER TABLE table_name AUTO_INCREMENT = 1")

还有其他方法吗?

提前致谢

您需要 truncate table:

TRUNCATE table `tablename`

Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all rows, or a sequence of DROP TABLE and CREATE TABLE statements.

Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.

更多信息请见 http://dev.mysql.com/doc/refman/5.7/en/truncate-table.html