SQLite 重置主键字段

SQLite 重置主键字段

问题描述:

我在 SQLite 中有几个表,我想弄清楚如何重置自动递增的数据库字段.

I have a few tables in SQLite and I am trying to figure out how to reset the auto-incremented database field.

我读到 DELETE FROM tablename 应该删除所有内容并将自动增量字段重置回 0,但是当我这样做时,它只会删除数据.当插入一条新记录时,自动增量会从删除前停止的地方开始.

I read that DELETE FROM tablename should delete everything and reset the auto-incremement field back to 0, but when I do this it just deletes the data. When a new record is inserted the autoincrement picks up where it left off before the delete.

我的ident字段属性如下:

  • 字段类型:整数
  • 字段标志:PRIMARY KEYAUTOINCREMENTUNIQUE

我在 SQLite Maestro 中构建表并且我也在 SQLite Maestro 中执行 DELETE 语句有关系吗?

Does it matter I built the table in SQLite Maestro and I am executing the DELETE statement in SQLite Maestro as well?

任何帮助都会很棒.

试试这个:

delete from your_table;    
delete from sqlite_sequence where name='your_table';

SQLite 自动增量

SQLite 跟踪最大的一个表曾经使用过的 ROWID特殊的 SQLITE_SEQUENCE.该SQLITE_SEQUENCE 表被创建并每当 a 时自动初始化包含一个的普通表AUTOINCREMENT 列已创建.该SQLITE_SEQUENCE 表的内容可以使用普通的 UPDATE 进行修改,INSERT 和 DELETE 语句.但是对此表进行修改可能会扰乱 AUTOINCREMENT密钥生成算法.确保你知道你在做什么之前进行此类更改.

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.