问题描述
我在 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 KEY
、AUTOINCREMENT
、UNIQUE
我在 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 跟踪最大的一个表曾经使用过的 ROWID特殊的 SQLITE_SEQUENCE
表.该SQLITE_SEQUENCE
表被创建并每当 a 时自动初始化包含一个的普通表AUTOINCREMENT 列已创建.该SQLITE_SEQUENCE 表的内容可以使用普通的 UPDATE 进行修改,INSERT 和 DELETE 语句.但是对此表进行修改可能会扰乱 AUTOINCREMENT密钥生成算法.确保你知道你在做什么之前进行此类更改.
这篇关于SQLite 重置主键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!