123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html><head>
- <title>SQLite Query Language: ON CONFLICT clause</title>
- <style type="text/css">
- body {
- margin: auto;
- font-family: Verdana, sans-serif;
- padding: 8px 1%;
- }
- a { color: #45735f }
- a:visited { color: #734559 }
- .logo { position:absolute; margin:3px; }
- .tagline {
- float:right;
- text-align:right;
- font-style:italic;
- width:240px;
- margin:12px;
- margin-top:58px;
- }
- .toolbar {
- font-variant: small-caps;
- text-align: center;
- line-height: 1.6em;
- margin: 0;
- padding:1px 8px;
- }
- .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
- .toolbar a:visited { color: white; }
- .toolbar a:hover { color: #80a796; background: white; }
- .content { margin: 5%; }
- .content dt { font-weight:bold; }
- .content dd { margin-bottom: 25px; margin-left:20%; }
- .content ul { padding:0px; padding-left: 15px; margin:0px; }
- /* rounded corners */
- .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
- .sw { background: url(images/sw.png) 0% 100% no-repeat }
- .ne { background: url(images/ne.png) 100% 0% no-repeat }
- .nw { background: url(images/nw.png) 0% 0% no-repeat }
- </style>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
-
- </head>
- <body>
- <div><!-- container div to satisfy validator -->
- <a href="lang.html">
- <h2 align="center">SQL As Understood By SQLite</h2></a><h1>ON CONFLICT clause</h1><h4><a href="syntaxdiagrams.html#conflict-clause">conflict-clause:</a></h4><blockquote> <img alt="syntax diagram conflict-clause" src="images/syntax/conflict-clause.gif"></img> </blockquote>
- <p>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.</p>
- <p>The syntax for the ON CONFLICT clause is as shown above for
- the CREATE TABLE command. For the INSERT and
- UPDATE commands, the keywords "ON CONFLICT" are replaced by "OR", to make
- the syntax seem more natural. 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.</p>
- <p>The ON CONFLICT clause specifies an algorithm used to resolve
- constraint conflicts. There are five choices: ROLLBACK, ABORT,
- FAIL, IGNORE, and REPLACE. The default algorithm is ABORT. This
- is what they mean:</p>
- <dl>
- <dt><b>ROLLBACK</b></dt>
- <dd><p>When a constraint violation occurs, an immediate ROLLBACK
- occurs, thus ending the current transaction, and the command aborts
- with a return code of SQLITE_CONSTRAINT. If no transaction is
- active (other than the implied transaction that is created on every
- command) then this algorithm works the same as ABORT.</p></dd>
- <dt><b>ABORT</b></dt>
- <dd><p>When a constraint violation occurs, the command backs out
- any prior changes it might have made and aborts with a return code
- of SQLITE_CONSTRAINT. But no ROLLBACK is executed so changes
- from prior commands within the same transaction
- are preserved. This is the default behavior.</p></dd>
- <dt><b>FAIL</b></dt>
- <dd><p>When a constraint violation occurs, the command aborts with a
- return code SQLITE_CONSTRAINT. But any changes to the database that
- the command made prior to encountering the constraint violation
- are preserved and are not backed out. 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.</p></dd>
- <dt><b>IGNORE</b></dt>
- <dd><p>When a constraint violation occurs, the one row that contains
- the constraint violation is not inserted or changed. But the command
- continues executing normally. Other rows before and after the row that
- contained the constraint violation continue to be inserted or updated
- normally. No error is returned.</p></dd>
- <dt><b>REPLACE</b></dt>
- <dd><p>When a UNIQUE constraint violation occurs, the pre-existing rows
- that are causing the constraint violation are removed prior to inserting
- or updating the current row. Thus the insert or update always occurs.
- The command continues executing normally. No error is returned.
- If a NOT NULL constraint violation occurs, the NULL value is replaced
- by the default value for that column. If the column has no default
- value, then the ABORT algorithm is used. If a CHECK constraint violation
- occurs then the IGNORE algorithm is used.</p>
- <p>When this conflict resolution strategy deletes rows in order to
- satisfy a constraint, <a href="lang_createtrigger.html">delete triggers</a> only fire if
- <a href="pragma.html#pragma_recursive_triggers">recursive triggers</a> are enabled.</p>
- <p>The <a href="c3ref/update_hook.html">update hook</a> is not invoked for rows that
- are deleted by an OR REPLACE resolution. Nor is the
- <a href="c3ref/changes.html">change counter</a> incremented.
- The exceptional behaviors defined in this paragraph might change
- in a future release.</p>
- </dl>
- <p>The algorithm specified in the OR clause of a INSERT or UPDATE
- overrides any algorithm specified in a CREATE TABLE.
- If no algorithm is specified anywhere, the ABORT algorithm is used.</p>
|