aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/conflict.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/conflict.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/conflict.tcl91
1 files changed, 91 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/conflict.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/conflict.tcl
new file mode 100644
index 0000000..2d11639
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/www/conflict.tcl
@@ -0,0 +1,91 @@
1#
2# Run this Tcl script to generate the constraint.html file.
3#
4set rcsid {$Id: conflict.tcl,v 1.4 2004/10/10 17:24:55 drh Exp $ }
5source common.tcl
6header {Constraint Conflict Resolution in SQLite}
7puts {
8<h1>Constraint Conflict Resolution in SQLite</h1>
9
10<p>
11In most SQL databases, if you have a UNIQUE constraint on
12a table and you try to do an UPDATE or INSERT that violates
13the constraint, the database will abort the operation in
14progress, back out any prior changes associated with
15UPDATE or INSERT command, and return an error.
16This is the default behavior of SQLite.
17Beginning with version 2.3.0, though, SQLite allows you to
18define alternative ways for dealing with constraint violations.
19This article describes those alternatives and how to use them.
20</p>
21
22<h2>Conflict Resolution Algorithms</h2>
23
24<p>
25SQLite defines five constraint conflict resolution algorithms
26as follows:
27</p>
28
29<dl>
30<dt><b>ROLLBACK</b></dt>
31<dd><p>When a constraint violation occurs, an immediate ROLLBACK
32occurs, thus ending the current transaction, and the command aborts
33with a return code of SQLITE_CONSTRAINT. If no transaction is
34active (other than the implied transaction that is created on every
35command) then this algorithm works the same as ABORT.</p></dd>
36
37<dt><b>ABORT</b></dt>
38<dd><p>When a constraint violation occurs, the command backs out
39any prior changes it might have made and aborts with a return code
40of SQLITE_CONSTRAINT. But no ROLLBACK is executed so changes
41from prior commands within the same transaction
42are preserved. This is the default behavior for SQLite.</p></dd>
43
44<dt><b>FAIL</b></dt>
45<dd><p>When a constraint violation occurs, the command aborts with a
46return code SQLITE_CONSTRAINT. But any changes to the database that
47the command made prior to encountering the constraint violation
48are preserved and are not backed out. For example, if an UPDATE
49statement encountered a constraint violation on the 100th row that
50it attempts to update, then the first 99 row changes are preserved
51by change to rows 100 and beyond never occur.</p></dd>
52
53<dt><b>IGNORE</b></dt>
54<dd><p>When a constraint violation occurs, the one row that contains
55the constraint violation is not inserted or changed. But the command
56continues executing normally. Other rows before and after the row that
57contained the constraint violation continue to be inserted or updated
58normally. No error is returned.</p></dd>
59
60<dt><b>REPLACE</b></dt>
61<dd><p>When a UNIQUE constraint violation occurs, the pre-existing row
62that caused the constraint violation is removed prior to inserting
63or updating the current row. Thus the insert or update always occurs.
64The command continues executing normally. No error is returned.</p></dd>
65</dl>
66
67<h2>Why So Many Choices?</h2>
68
69<p>SQLite provides multiple conflict resolution algorithms for a
70couple of reasons. First, SQLite tries to be roughly compatible with as
71many other SQL databases as possible, but different SQL database
72engines exhibit different conflict resolution strategies. For
73example, PostgreSQL always uses ROLLBACK, Oracle always uses ABORT, and
74MySQL usually uses FAIL but can be instructed to use IGNORE or REPLACE.
75By supporting all five alternatives, SQLite provides maximum
76portability.</p>
77
78<p>Another reason for supporting multiple algorithms is that sometimes
79it is useful to use an algorithm other than the default.
80Suppose, for example, you are
81inserting 1000 records into a database, all within a single
82transaction, but one of those records is malformed and causes
83a constraint error. Under PostgreSQL or Oracle, none of the
841000 records would get inserted. In MySQL, some subset of the
85records that appeared before the malformed record would be inserted
86but the rest would not. Neither behavior is especially helpful.
87What you really want is to use the IGNORE algorithm to insert
88all but the malformed record.</p>
89
90}
91footer $rcsid