• <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. 如果表中的一行不存在,我如何更新或插入它?

        How do I UPDATE a row in a table or INSERT it if it doesn#39;t exist?(如果表中的一行不存在,我如何更新或插入它?)
        <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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)

                  <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>
                            <tbody id='YMbWj'></tbody>
                          1. <tfoot id='YMbWj'></tfoot>