conflict.tcl 4.1 KB

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