sqlite ON CONFLICT difference between ABORT and FAIL

放肆的年华 提交于 2019-12-23 06:57:37

问题


From http://www.sqlite.org/lang_conflict.html

ABORT When an applicable constraint violation occurs, the ABORT resolution algorithm aborts the current SQL statement with an SQLITE_CONSTRAIT error and backs out any changes made by the current SQL statement; but changes caused by prior SQL statements within the same transaction are preserved and the transaction remains active. This is the default behavior and the behavior proscribed the SQL standard.

FAIL When an applicable constraint violation occurs, the FAIL resolution algorithm aborts the current SQL statement with an SQLITE_CONSTRAINT error. But the FAIL resolution does not back out prior changes of the SQL statement that failed nor does it end the transaction. For example, if an UPDATE statement encountered a constraint violation on the 100th row that it attempts to update, then the first 99 row changes are preserved but changes to rows 100 and beyond never occur.

Both preserve changes made before the statement that caused constraint violation and do not end transaction. So, I suppose the only difference is that FAIL resolution does not let further changes to be made, while ABORT does only back up only conflicting statement. Did I get right?


回答1:


The answer is simple: FAIL does not rollback changes done by the current statement.

Consider this 2 tables:

CREATE TABLE IF NOT EXISTS constFAIL (num UNIQUE ON CONFLICT FAIL);
CREATE TABLE IF NOT EXISTS constABORT (num UNIQUE ON CONFLICT ABORT);
INSERT INTO constFAIL VALUES (1),(3),(4),(5),(6),(7),(8),(9),(10);
INSERT INTO constABORT VALUES (1),(3),(4),(5),(6),(7),(8),(9),(10);

The statement

UPDATE constABORT SET num=num+1 WHERE num<10

will fail and change nothing. But this satement

UPDATE constFAIL SET num=num+1 WHERE num<10

will update the first row, then fail and leave the 1 row updated, so the new values are 2, 3, 4, 5, 6, 7, 8, 9, 10



来源:https://stackoverflow.com/questions/11370712/sqlite-on-conflict-difference-between-abort-and-fail

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