devroom.io/content/posts/2007-11-30-mysql-reset-the-auto-increment-value-of-a-table.md

21 lines
1.1 KiB
Markdown
Raw Normal View History

2015-03-26 11:28:08 +00:00
+++
date = "2007-11-30"
title = "MySQL: (Re)set the auto-increment value of a table"
tags = ["General", "Blog", "Databases", "MySQL", "development"]
slug = "mysql-reset-the-auto-increment-value-of-a-table"
2017-09-11 12:20:15 +00:00
description = "Let's start over by resetting MySQL's auto increment counter."
2015-03-26 11:28:08 +00:00
+++
Sometimes it's necessary to set the starting point of a MySQL auto-increment value.
Normally, MySQL starts auto-incrementing at 1. But let's say you want to start at 10.000, because you want at least a five figure number. You can use the following query to set the MySQL auto-index:
<pre lang="sql">ALTER TABLE some_table AUTO_INCREMENT=10000</pre>
If you want to delete all records from your table and restart auto-index at 1, you might be tempted to run a DELETE query, followed by the above example, setting the auto increment value to 1. There is a shortcut, however:
<pre lang="sql">TRUNCATE TABLE some_table</pre>
This will basically reset the table, deleting all data and resetting the auto increment index. Do not that the truncate command is a hard-reset option. For instance, any triggers "ON DELETE" will not be fired when using truncate.