Home >Database >Mysql Tutorial >How to Reset an Auto-Incremented Primary Key in SQLite?

How to Reset an Auto-Incremented Primary Key in SQLite?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-15 14:27:44798browse

How to Reset an Auto-Incremented Primary Key in SQLite?

Resetting the Auto-Incrementing Primary Key in SQLite

Simply deleting data from an SQLite table using DELETE FROM doesn't reset the auto-incrementing primary key back to 1. To achieve this, you need a two-step process:

<code class="language-sql">DELETE FROM your_table;
DELETE FROM sqlite_sequence WHERE name = 'your_table';</code>

The first statement removes all rows from your table. The second, and crucial, statement deletes the entry from the sqlite_sequence table. This system table tracks the highest ROWID for each table with an auto-increment column.

Understanding SQLite's Autoincrement Mechanism

SQLite uses sqlite_sequence to manage auto-incrementing primary keys. When a new row is added to a table with an AUTOINCREMENT column, SQLite consults sqlite_sequence to find the largest existing ROWID and increments it.

Therefore, directly manipulating sqlite_sequence affects the auto-increment sequence. Proceed with caution and fully understand the potential impact before making any changes.

The above is the detailed content of How to Reset an Auto-Incremented Primary Key in SQLite?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn