加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite ON CONFLICT ABORT和FAIL之间的区别

发布时间:2020-12-12 19:09:01 所属栏目:百科 来源:网络整理
导读:从 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
从 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.

两者都保留在导致约束违规并且不结束事务的语句之前所做的更改.因此,我认为唯一的区别是FAIL解决方案不允许进行进一步的更改,而ABORT只备份冲突的语句.我做对了吗?

答案很简单:FAIL不会回滚当前语句所做的更改.

考虑这两个表:

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),(10);

该声明

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

会失败并且什么都不改变.
但是这个声明

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

将更新第一行,然后失败并保持1行更新,因此新值为2,3,4,5,6,7,8,9,10

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读