通过删除未使用的 PRIMARY KEY AUTOINCREMENTAL INTEGER 来压缩 DB
Compress DB by removing unused PRIMARY KEY AUTOINCREMENTAL INTEGER
使用 SQLite3 我得到了关注 table:
| Idx | Foo | Bar |
|-----|-------|---------|
| 1 | It's |something|
| 2 | and | even |
| 5 | more |wildcard |
Idx 是 INTEGER PRIMARY KEY AUTOINCREMENT。
如您所见,Idx 3 和 4 由于已被删除而未使用。我可以让这些 Idx 再次可用吗?
我对 SQL 不是很确定,但是 afaik,毫无疑问,我需要自动增量来识别该行,其他每一列都可以有双峰。
如 SQLite documentation 所述:
If a table contains a column of type INTEGER PRIMARY KEY, then that
column becomes an alias for the ROWID. [...] With AUTOINCREMENT, rows
with automatically selected ROWIDs are guaranteed to have ROWIDs that
have never been used before by the same table in the same database.
And the automatically generated ROWIDs are guaranteed to be
monotonically increasing.
所以你的问题是"Can I make these Idx usable again?"
答案是:是的,但只是明确地,参见:
sqlite> create table testtable(idx INTEGER PRIMARY KEY AUTOINCREMENT, foo varchar(100), bar varchar(100));
sqlite> insert into testtable (foo, bar) values ('a', 'b');
sqlite> insert into testtable (foo, bar) values ('c', 'd');
sqlite> insert into testtable (foo, bar) values ('e', 'd');
sqlite> insert into testtable (foo, bar) values ('g', 'g');
sqlite> select * from testtable;
1|a|b
2|c|d
3|e|d
4|g|g
sqlite> delete from testtable where idx = 2;
sqlite> select * from testtable;
1|a|b
3|e|d
4|g|g
sqlite> insert into testtable (foo, bar) values ('h', 'i');
sqlite> select * from testtable;
1|a|b
3|e|d
4|g|g
5|h|i
sqlite> insert into testtable (idx, foo, bar) values (2, 'j', 'k');
sqlite> select * from testtable;
1|a|b
2|j|k
3|e|d
4|g|g
5|h|i
sqlite>
使用 SQLite3 我得到了关注 table:
| Idx | Foo | Bar |
|-----|-------|---------|
| 1 | It's |something|
| 2 | and | even |
| 5 | more |wildcard |
Idx 是 INTEGER PRIMARY KEY AUTOINCREMENT。
如您所见,Idx 3 和 4 由于已被删除而未使用。我可以让这些 Idx 再次可用吗?
我对 SQL 不是很确定,但是 afaik,毫无疑问,我需要自动增量来识别该行,其他每一列都可以有双峰。
如 SQLite documentation 所述:
If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID. [...] With AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. And the automatically generated ROWIDs are guaranteed to be monotonically increasing.
所以你的问题是"Can I make these Idx usable again?" 答案是:是的,但只是明确地,参见:
sqlite> create table testtable(idx INTEGER PRIMARY KEY AUTOINCREMENT, foo varchar(100), bar varchar(100));
sqlite> insert into testtable (foo, bar) values ('a', 'b');
sqlite> insert into testtable (foo, bar) values ('c', 'd');
sqlite> insert into testtable (foo, bar) values ('e', 'd');
sqlite> insert into testtable (foo, bar) values ('g', 'g');
sqlite> select * from testtable;
1|a|b
2|c|d
3|e|d
4|g|g
sqlite> delete from testtable where idx = 2;
sqlite> select * from testtable;
1|a|b
3|e|d
4|g|g
sqlite> insert into testtable (foo, bar) values ('h', 'i');
sqlite> select * from testtable;
1|a|b
3|e|d
4|g|g
5|h|i
sqlite> insert into testtable (idx, foo, bar) values (2, 'j', 'k');
sqlite> select * from testtable;
1|a|b
2|j|k
3|e|d
4|g|g
5|h|i
sqlite>