重置SQLite3/MySQL中的行数计数

发布于 2024-09-13 18:24:00 字数 157 浏览 16 评论 0原文

我正在使用 SQLite3。我使用整数作为主 ID 加载一个包含 30 行的表,并且它会自动递增。

现在,我从表中删除所有行,然后将一些新信息重新加载到表中。

问题是:行计数(我的 PrimaryID)现在从 31 开始。有什么方法可以让我从数字 1 开始加载新行吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水染的天色ゝ 2024-09-20 18:24:00

SQLite

使用:

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

文档

SQLite 使用特殊的 SQLITE_SEQUENCE 表来跟踪表曾经保存的最大 ROWID。每当创建包含 AUTOINCREMENT 列的普通表时,都会自动创建并初始化 SQLITE_SEQUENCE 表。可以使用普通的 UPDATE、INSERT 和 DELETE 语句修改 SQLITE_SEQUENCE 表的内容。但是对此表进行修改可能会扰乱 AUTOINCREMENT 键生成算法。在进行此类更改之前,请确保您知道自己在做什么。

找到答案:SQLite Reset Primary Key Field

MySQL

使用:

ALTER TABLE tbl AUTO_INCREMENT = 1;

无论哪种情况,数据库不关心 id 号是否是连续的 - 只关心值是唯一的。如果用户永远看不到主键值(他们不应该看到,因为数据可以更改并且不会始终处于该主键值),我不会为这些选项而烦恼。

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.

︶ ̄淡然 2024-09-20 18:24:00

对于 MySQL:

使用 TRUNCATE TABLE tablename 清空表(删除所有记录)并重置自动递增计数。

如果您只想重置计数,也可以使用ALTER TABLE tablename AUTO_INCRMENT = 0;

对于 SQLite:

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

参考资料

SQLite 自动增量
MySQL 自动增量

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

土豪我们做朋友吧 2024-09-20 18:24:00

对于SQLite使用(不需要删除和创建表)

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

对于MySql使用

ALTER TABLE table_name AUTO_INCREMENT = 1;

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;
乖乖 2024-09-20 18:24:00

在这种情况下,您不应使用AUTOINCRMENT。只需将主键定义为INTEGER PRIMARY KEY,在执行DELETE FROM 查询后计数将重置为 1。如果没有 AUTOINCRMENT,只要表中的空间没有用完,默认行为仍然是主键的自动增量(在这种情况下,旧的已删除值将被重用)。

更多信息请参阅SQLite 自动增量文档。

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.

怼怹恏 2024-09-20 18:24:00
ALTER TABLE tbl AUTO_INCREMENT = 0;
ALTER TABLE tbl AUTO_INCREMENT = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文