20

I am using SQLite3. I load a table with say 30 rows using integer as Primary ID and it auto-increments.

Now I delete all the rows from the table and then, reload some new information onto the table.

Problem is: the row count (my PrimaryID) now starts with 31. Is there any way that I can start loading new rows from the number 1 onwards?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Maddy
  • 2,520
  • 14
  • 44
  • 64

5 Answers5

38

SQLite

Use:

DELETE FROM your_table;    
DELETE FROM sqlite_sequence WHERE name = 'your_table';

Documentation

SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. The SQLITE_SEQUENCE table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes.

Found the answer on SO: SQLite Reset Primary Key Field

MySQL

Use:

ALTER TABLE tbl AUTO_INCREMENT = 1;

In either case, the database doesn't care if the id numbers are sequencial - only that the values are unique. If users never see the primary key value (they shouldn't, because the data can change & won't always be at that primary key value), I wouldn't bother with these options.

Community
  • 1
  • 1
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
12

For MySQL:

Use TRUNCATE TABLE tablename to empty the table (delete all records) and reset auto increment count.

You can also use ALTER TABLE tablename AUTO_INCREMENT = 0; if you just want to reset the count.

For SQLite:

DELETE FROM tablename;
DELETE FROM SQLITE_SEQUENCE WHERE name='tablename';

References

SQLite AutoIncrement
MySQL AutoIncrement

quantumSoup
  • 27,197
  • 9
  • 43
  • 57
  • Setting the auto_increment to zero on MySQL doesn't reset the value to one; it adopts the next highest value based on the existing ones. – OMG Ponies Oct 02 '10 at 22:05
8

For SQLite use (not need to delete and create the table)

UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='table_name';

For MySql use

ALTER TABLE table_name AUTO_INCREMENT = 1;
Wh1T3h4Ck5
  • 8,399
  • 9
  • 59
  • 79
williambarau
  • 491
  • 5
  • 4
7

You should not use AUTOINCREMENT in this case. Simply define your primary key as INTEGER PRIMARY KEY and the count will be reset to 1 after a DELETE FROM query. Without AUTOINCREMENT, the default behaviour will still be an automatic increment of the primary key as long as you don't run out of space in your table (in that case, old - deleted - values will be reused).

More information available in the SQLite Autoincrement document.

Tom Ace
  • 136
  • 1
  • 1
  • 1
    by far the most accurate answer. Per the sqlite [website](https://www.sqlite.org/autoinc.html), "The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. **It is usually not needed.**" (emphasis added) – Christopher Pearson Apr 29 '15 at 04:50
0
ALTER TABLE tbl AUTO_INCREMENT = 0;
websch01ar
  • 2,103
  • 2
  • 13
  • 22