The ON CONFLICT clause is not a separate SQL command. It is a non-standard clause that can appear in many other SQL commands. It is given its own section in this document because it is not part of standard SQL and therefore might not be familiar.
R-31884-19145:[The syntax for the ON CONFLICT clause is as shown above for the CREATE TABLE command. ] R-26917-59594:[For the INSERT and UPDATE commands, the keywords "ON CONFLICT" are replaced by "OR" so that the syntax reads more naturally. ] For example, instead of "INSERT ON CONFLICT IGNORE" we have "INSERT OR IGNORE". The keywords change but the meaning of the clause is the same either way.
The ON CONFLICT clause applies to UNIQUE, NOT NULL, CHECK, and PRIMARY KEY constraints. The ON CONFLICT algorithm does not apply to FOREIGN KEY constraints. There are five conflict resolution algorithm choices: ROLLBACK, ABORT, FAIL, IGNORE, and REPLACE. R-55069-12282:[The default conflict resolution algorithm is ABORT. ] This is what they mean:
R-31881-13880:[When an applicable constraint violation occurs, the ROLLBACK resolution algorithm aborts the current SQL statement with an SQLITE_CONSTRAINT error and rolls back the current transaction. ] R-42089-41020:[If no transaction is active (other than the implied transaction that is created on every command) then the ROLLBACK resolution algorithm works the same as the ABORT algorithm. ]
R-47224-48532:[When an applicable constraint violation occurs, the ABORT resolution algorithm aborts the current SQL statement with an SQLITE_CONSTRAINT 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 specified by the SQL standard.
R-45095-63572:[When an applicable constraint violation occurs, the FAIL resolution algorithm aborts the current SQL statement with an SQLITE_CONSTRAINT error. ] R-31882-08811:[But the FAIL resolution does not back out prior changes of the SQL statement that failed nor does it end the transaction. ] R-48584-41074:[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. ]
R-50299-05307:[When an applicable constraint violation occurs, the IGNORE resolution algorithm skips the one row that contains the constraint violation and continues processing subsequent rows of the SQL statement as if nothing went wrong. ] Other rows before and after the row that contained the constraint violation are inserted or updated normally. R-52324-04086:[No error is returned when the IGNORE conflict resolution algorithm is used. ]
R-54702-17780:[When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally. ] R-12527-00403:[If a NOT NULL constraint violation occurs, the REPLACE conflict resolution replaces the NULL value with the default value for that column, or if the column has no default value, then the ABORT algorithm is used. ] R-15569-63625:[If a CHECK constraint violation occurs, the REPLACE conflict resolution algorithm always works like ABORT. ]
R-23498-38103:[When the REPLACE conflict resolution strategy deletes rows in order to satisfy a constraint, delete triggers fire if and only if recursive triggers are enabled. ]
R-40235-17052:[The update hook is not invoked for rows that are deleted by the REPLACE conflict resolution strategy. ] R-37019-57602:[Nor does REPLACE increment the change counter. ] The exceptional behaviors defined in this paragraph might change in a future release.
R-33948-22286:[The algorithm specified in the OR clause of an INSERT or UPDATE overrides any algorithm specified in a CREATE TABLE. ] R-05239-12850:[If no algorithm is specified anywhere, the ABORT algorithm is used. ]