sqlite 在回滚后重复主键自动增量值
sqlite repeats primary key autoincrement value after rollback
我预计 used 和 rollback(ed) PK 值将被 sqlite 丢弃,但不会以这种方式发生。这是测试:
PRAGMA foreign_keys = ON;
BEGIN TRANSACTION;
INSERT INTO _UIDlgt (id, TS) VALUES (null, '1421248181');
select max(id) from _UIDlgt; -- shows 6 ok, there were previous records
ROLLBACK;
select max(id) from _UIDlgt; -- shows 5 ok, was previous max value
INSERT INTO _UIDlgt (id, TS) VALUES (null, '1421248181');
select max(id) from _UIDlgt; -- shows 6 I expect 7 !!!!
这是table创作:
CREATE TABLE _UIDlgt (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
TS INTEGER NOT NULL
);
手动 says 失败的插入会跳过尝试使用的 PK 值,但注意回滚。
问题是:是否有一些设置来管理此行为? 还是技巧?
Ms Sql 服务器按我预期的方式运行。 Sqlite版本是PHP5.4.11
下的3.7.7.1
SQLite docs 中有一些微妙的语言。 (强调已添加。)
In other words, the purpose of AUTOINCREMENT is to prevent the reuse
of ROWIDs from previously deleted rows.
这与 client/server SQL dbms 有点不同,后者必须处理并发写入。 SQLite 需要 exclusive lock for writes.
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.
这意味着保证与提交的行有关。这意味着未提交行的 ID 号在 table.
中不是 "used before"(在这个意义上)
However, if an insert fails due to (for example) a uniqueness
constraint, the ROWID of the failed insertion attempt might not be
reused on subsequent inserts, resulting in gaps in the ROWID sequence.
保留重复使用的可能性。
我预计 used 和 rollback(ed) PK 值将被 sqlite 丢弃,但不会以这种方式发生。这是测试:
PRAGMA foreign_keys = ON;
BEGIN TRANSACTION;
INSERT INTO _UIDlgt (id, TS) VALUES (null, '1421248181');
select max(id) from _UIDlgt; -- shows 6 ok, there were previous records
ROLLBACK;
select max(id) from _UIDlgt; -- shows 5 ok, was previous max value
INSERT INTO _UIDlgt (id, TS) VALUES (null, '1421248181');
select max(id) from _UIDlgt; -- shows 6 I expect 7 !!!!
这是table创作:
CREATE TABLE _UIDlgt (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
TS INTEGER NOT NULL
);
手动 says 失败的插入会跳过尝试使用的 PK 值,但注意回滚。
问题是:是否有一些设置来管理此行为? 还是技巧?
Ms Sql 服务器按我预期的方式运行。 Sqlite版本是PHP5.4.11
下的3.7.7.1SQLite docs 中有一些微妙的语言。 (强调已添加。)
In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
这与 client/server SQL dbms 有点不同,后者必须处理并发写入。 SQLite 需要 exclusive lock for writes.
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.
这意味着保证与提交的行有关。这意味着未提交行的 ID 号在 table.
中不是 "used before"(在这个意义上)However, if an insert fails due to (for example) a uniqueness constraint, the ROWID of the failed insertion attempt might not be reused on subsequent inserts, resulting in gaps in the ROWID sequence.
保留重复使用的可能性。