How to reseed an an auto increment column in a SQLite database?

妖精的绣舞 提交于 2019-12-21 07:27:09

问题


Is it possible to reseed an auto increment column in a SQLite database, and if so, how is this done?

ie. The equivalent of DBCC CHECKIDENT ('MyTable', RESEED, 1) in SQL Server.


回答1:


In SQLite there is a table named SQLITE_SEQUENCE, which tracks the largest RowId value that a table has. You can do insert, updates and deletes on this table. For example, to mimic similar functionality as the TRUNCATE TABLE statement SQL Server you could something like:

DELETE FROM MyTableName;
DELETE FROM SQLITE_SEQUENCE WHERE NAME = 'MyTableName';

In the above example all data from MyTableName is removed, and the auto increment rowid is reset by removing the value from the SQLITE_SEQUENCE table. See the documentation for AUTOINCREMENT for more information.




回答2:


DELETE 
FROM MyTableName

select *
from SQLITE_SEQUENCE

update SQLITE_SEQUENCE 
set seq = 0
where name ='MyTableName'


来源:https://stackoverflow.com/questions/3122865/how-to-reseed-an-an-auto-increment-column-in-a-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!