2
0

lang_conflict.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: ON CONFLICT clause</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <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>
  47. <p>The ON CONFLICT clause is not a separate SQL command. It is a
  48. non-standard clause that can appear in many other SQL commands.
  49. It is given its own section in this document because it is not
  50. part of standard SQL and therefore might not be familiar.</p>
  51. <p>The syntax for the ON CONFLICT clause is as shown above for
  52. the CREATE TABLE command. For the INSERT and
  53. UPDATE commands, the keywords "ON CONFLICT" are replaced by "OR", to make
  54. the syntax seem more natural. For example, instead of
  55. "INSERT ON CONFLICT IGNORE" we have "INSERT OR IGNORE".
  56. The keywords change but the meaning of the clause is the same
  57. either way.</p>
  58. <p>The ON CONFLICT clause specifies an algorithm used to resolve
  59. constraint conflicts. There are five choices: ROLLBACK, ABORT,
  60. FAIL, IGNORE, and REPLACE. The default algorithm is ABORT. This
  61. is what they mean:</p>
  62. <dl>
  63. <dt><b>ROLLBACK</b></dt>
  64. <dd><p>When a constraint violation occurs, an immediate ROLLBACK
  65. occurs, thus ending the current transaction, and the command aborts
  66. with a return code of SQLITE_CONSTRAINT. If no transaction is
  67. active (other than the implied transaction that is created on every
  68. command) then this algorithm works the same as ABORT.</p></dd>
  69. <dt><b>ABORT</b></dt>
  70. <dd><p>When a constraint violation occurs, the command backs out
  71. any prior changes it might have made and aborts with a return code
  72. of SQLITE_CONSTRAINT. But no ROLLBACK is executed so changes
  73. from prior commands within the same transaction
  74. are preserved. This is the default behavior.</p></dd>
  75. <dt><b>FAIL</b></dt>
  76. <dd><p>When a constraint violation occurs, the command aborts with a
  77. return code SQLITE_CONSTRAINT. But any changes to the database that
  78. the command made prior to encountering the constraint violation
  79. are preserved and are not backed out. For example, if an UPDATE
  80. statement encountered a constraint violation on the 100th row that
  81. it attempts to update, then the first 99 row changes are preserved
  82. but changes to rows 100 and beyond never occur.</p></dd>
  83. <dt><b>IGNORE</b></dt>
  84. <dd><p>When a constraint violation occurs, the one row that contains
  85. the constraint violation is not inserted or changed. But the command
  86. continues executing normally. Other rows before and after the row that
  87. contained the constraint violation continue to be inserted or updated
  88. normally. No error is returned.</p></dd>
  89. <dt><b>REPLACE</b></dt>
  90. <dd><p>When a UNIQUE constraint violation occurs, the pre-existing rows
  91. that are causing the constraint violation are removed prior to inserting
  92. or updating the current row. Thus the insert or update always occurs.
  93. The command continues executing normally. No error is returned.
  94. If a NOT NULL constraint violation occurs, the NULL value is replaced
  95. by the default value for that column. If the column has no default
  96. value, then the ABORT algorithm is used. If a CHECK constraint violation
  97. occurs then the IGNORE algorithm is used.</p>
  98. <p>When this conflict resolution strategy deletes rows in order to
  99. satisfy a constraint, <a href="lang_createtrigger.html">delete triggers</a> only fire if
  100. <a href="pragma.html#pragma_recursive_triggers">recursive triggers</a> are enabled.</p>
  101. <p>The <a href="c3ref/update_hook.html">update hook</a> is not invoked for rows that
  102. are deleted by an OR REPLACE resolution. Nor is the
  103. <a href="c3ref/changes.html">change counter</a> incremented.
  104. The exceptional behaviors defined in this paragraph might change
  105. in a future release.</p>
  106. </dl>
  107. <p>The algorithm specified in the OR clause of a INSERT or UPDATE
  108. overrides any algorithm specified in a CREATE TABLE.
  109. If no algorithm is specified anywhere, the ABORT algorithm is used.</p>