• <i id='cM6OA'><tr id='cM6OA'><dt id='cM6OA'><q id='cM6OA'><span id='cM6OA'><b id='cM6OA'><form id='cM6OA'><ins id='cM6OA'></ins><ul id='cM6OA'></ul><sub id='cM6OA'></sub></form><legend id='cM6OA'></legend><bdo id='cM6OA'><pre id='cM6OA'><center id='cM6OA'></center></pre></bdo></b><th id='cM6OA'></th></span></q></dt></tr></i><div id='cM6OA'><tfoot id='cM6OA'></tfoot><dl id='cM6OA'><fieldset id='cM6OA'></fieldset></dl></div>
      <bdo id='cM6OA'></bdo><ul id='cM6OA'></ul>

    <small id='cM6OA'></small><noframes id='cM6OA'>

      1. <legend id='cM6OA'><style id='cM6OA'><dir id='cM6OA'><q id='cM6OA'></q></dir></style></legend>
      2. <tfoot id='cM6OA'></tfoot>
      3. 如果表中的一行不存在,我如何更新或插入它?

        时间:2023-06-02
        <legend id='lRAfk'><style id='lRAfk'><dir id='lRAfk'><q id='lRAfk'></q></dir></style></legend>
          <bdo id='lRAfk'></bdo><ul id='lRAfk'></ul>

          1. <i id='lRAfk'><tr id='lRAfk'><dt id='lRAfk'><q id='lRAfk'><span id='lRAfk'><b id='lRAfk'><form id='lRAfk'><ins id='lRAfk'></ins><ul id='lRAfk'></ul><sub id='lRAfk'></sub></form><legend id='lRAfk'></legend><bdo id='lRAfk'><pre id='lRAfk'><center id='lRAfk'></center></pre></bdo></b><th id='lRAfk'></th></span></q></dt></tr></i><div id='lRAfk'><tfoot id='lRAfk'></tfoot><dl id='lRAfk'><fieldset id='lRAfk'></fieldset></dl></div>
          2. <small id='lRAfk'></small><noframes id='lRAfk'>

              <tfoot id='lRAfk'></tfoot>
                  <tbody id='lRAfk'></tbody>

                  本文介绍了如果表中的一行不存在,我如何更新或插入它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下计数器表:

                  CREATE TABLE cache (
                      key text PRIMARY KEY,
                      generation int
                  );
                  

                  如果相应的行尚不存在,我想增加其中一个计数器,或将其设置为零.有没有办法在标准 SQL 中没有并发问题的情况下做到这一点?操作有时是事务的一部分,有时是独立的.

                  I would like to increment one of the counters, or set it to zero if the corresponding row doesn't exist yet. Is there a way to do this without concurrency issues in standard SQL? The operation is sometimes part of a transaction, sometimes separate.

                  如果可能,SQL 必须在未修改的情况下在 SQLite、PostgreSQL 和 MySQL 上运行.

                  The SQL must run unmodified on SQLite, PostgreSQL and MySQL, if possible.

                  搜索产生了几个想法,这些想法要么受到并发问题的影响,要么特定于数据库:

                  A search yielded several ideas which either suffer from concurrency issues, or are specific to a database:

                  • 尝试INSERT一个新行,如果有错误,UPDATE.不幸的是,INSERT 上的错误中止了当前事务.

                  • Try to INSERT a new row, and UPDATE if there was an error. Unfortunately, the error on INSERT aborts the current transaction.

                  UPDATE 行,如果没有行被修改,INSERT 一个新行.

                  UPDATE the row, and if no rows were modified, INSERT a new row.

                  MySQL 有一个 ON DUPLICATE KEY UPDATE 子句.

                  MySQL has an ON DUPLICATE KEY UPDATE clause.

                  感谢您的所有精彩回复.看起来 Paul 是对的,并且没有一种单一的、可移植的方式来做到这一点.这让我很惊讶,因为这听起来像是一项非常基本的操作.

                  Thanks for all the great replies. It looks like Paul is right, and there's not a single, portable way of doing this. That's quite surprising to me, as it sounds like a very basic operation.

                  推荐答案

                  MySQL(以及随后的 SQLite)也支持 REPLACE INTO 语法:

                  MySQL (and subsequently SQLite) also support the REPLACE INTO syntax:

                  REPLACE INTO my_table (pk_id, col1) VALUES (5, '123');
                  

                  这会自动识别主键并找到要更新的匹配行,如果找不到则插入新行.

                  This automatically identifies the primary key and finds a matching row to update, inserting a new one if none is found.

                  这篇关于如果表中的一行不存在,我如何更新或插入它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在不依赖数据库的情况下启动 spring-boot 应用程序? 下一篇:是否可以使用 MySQL 对多列进行 GROUP BY?

                  相关文章

                  <small id='YMbWj'></small><noframes id='YMbWj'>

                • <i id='YMbWj'><tr id='YMbWj'><dt id='YMbWj'><q id='YMbWj'><span id='YMbWj'><b id='YMbWj'><form id='YMbWj'><ins id='YMbWj'></ins><ul id='YMbWj'></ul><sub id='YMbWj'></sub></form><legend id='YMbWj'></legend><bdo id='YMbWj'><pre id='YMbWj'><center id='YMbWj'></center></pre></bdo></b><th id='YMbWj'></th></span></q></dt></tr></i><div id='YMbWj'><tfoot id='YMbWj'></tfoot><dl id='YMbWj'><fieldset id='YMbWj'></fieldset></dl></div>
                    <legend id='YMbWj'><style id='YMbWj'><dir id='YMbWj'><q id='YMbWj'></q></dir></style></legend>

                      <bdo id='YMbWj'></bdo><ul id='YMbWj'></ul>
                    1. <tfoot id='YMbWj'></tfoot>