AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 44215
Accepted
Jack Douglas
Jack Douglas
Asked: 2013-06-11 07:25:19 +0800 CST2013-06-11 07:25:19 +0800 CST 2013-06-11 07:25:19 +0800 CST

如何在一个语句中组合“删除”和“插入”操作?

  • 772

对于以下数据,我希望能够删除一些行并插入其他行,给出以下结果。这是否可以通过单个语句(例如merge语句)来实现?

create table product(product_id integer primary key);
insert into product(product_id) values(1);
insert into product(product_id) values(2);
insert into product(product_id) values(3);

create table split( parent_id integer not null references product, 
                    child_id integer not null references product,
                    primary key(parent_id, child_id) );

insert into split(parent_id, child_id) values(1,2);
insert into split(parent_id, child_id) values(1,3);

create table sale(sale_at date, product_id integer references product);

insert into sale(sale_at, product_id) values(sysdate, 1);
insert into sale(sale_at, product_id) values(sysdate, 1);
insert into sale(sale_at, product_id) values(sysdate, 1);
insert into sale(sale_at, product_id) values(sysdate, 2);
insert into sale(sale_at, product_id) values(sysdate, 2);

 

select sale_at, product_id from sale where product_id not in (select parent_id from split)
union all
select sale_at, child_id from sale join split on(parent_id=product_id);

/*
|                     SALE_AT | PRODUCT_ID |
--------------------------------------------
| June, 10 2013 15:18:22+0000 |          2 |
| June, 10 2013 15:18:22+0000 |          2 |
| June, 10 2013 15:18:22+0000 |          3 |
| June, 10 2013 15:18:22+0000 |          2 |
| June, 10 2013 15:18:22+0000 |          3 |
| June, 10 2013 15:18:22+0000 |          2 |
| June, 10 2013 15:18:22+0000 |          3 |
| June, 10 2013 15:18:22+0000 |          2 |
*/

( SQLFiddle )

- - 编辑:

澄清一下,我试图用一个单一的merge语句得到的效果是这样的:

insert into sale(sale_at, product_id)
select sale_at, child_id from sale join split on parent_id=product_id;

delete from sale where product_id in (select parent_id from split);

我想要一个单语句解决方案的原因是为了防止另一个事务在主事务insert之间插入/提交数据的竞争条件。delete

oracle oracle-11g-r2
  • 1 1 个回答
  • 21767 Views

1 个回答

  • Voted
  1. Best Answer
    Vincent Malgrat
    2013-06-12T06:42:00+08:002013-06-12T06:42:00+08:00

    你可以一口气完成,有点:

    SQL> MERGE INTO sale s
      2  USING (SELECT ROWID rid, sale_at, product_id
      3           FROM sale
      4          WHERE product_id IN (SELECT parent_id FROM split)
      5         UNION ALL
      6         SELECT CAST (NULL AS ROWID) rid, sale_at, child_id FROM sale
      7           JOIN split ON (parent_id = product_id)) m
      8  ON (s.ROWID = m.rid)
      9  WHEN MATCHED THEN
     10     UPDATE SET s.sale_at = m.sale_at
     11     DELETE WHERE 1 = 1
     12  WHEN NOT MATCHED THEN
     13     INSERT VALUES (m.sale_at, m.product_id);
    
    Done
    
    SQL> select * from sale;
    
    SALE_AT                                  PRODUCT_ID
    ----------- ---------------------------------------
    11/06/2013                                        2
    11/06/2013                                        2
    11/06/2013                                        3
    11/06/2013                                        3
    11/06/2013                                        3
    11/06/2013                                        2
    11/06/2013                                        2
    11/06/2013                                        2
    

    这将从销售中删除拆分中存在的行,并将其替换为相应的拆分产品。

    你也可以这样写:

    SQL> MERGE INTO sale s
      2  USING (SELECT CASE WHEN row_number() over (PARTITION BY s.rowid
      3                                             ORDER BY sp.rowid) = 1
      4                     THEN  s.rowid
      5                END rid,
      6                s.sale_at, sp.parent_id, sp.child_id
      7           FROM split sp
      8           JOIN sale s
      9             ON sp.parent_id = s.product_id) m
     10  ON (s.rowid = m.rid)
     11  WHEN MATCHED THEN UPDATE SET s.product_id = m.child_id
     12  WHEN NOT MATCHED THEN INSERT VALUES (m.sale_at, m.child_id);
    

    在这里,销售中的行将被其第一个拆分组件替换(更新),并且将插入其他组件。

    • 10

相关问题

  • Oracle 中的数据库备份 - 导出数据库还是使用其他工具?

  • ORDER BY 使用文本列的自定义优先级

  • 舒服的sqlplus界面?[关闭]

  • 如何在数据库中找到最新的 SQL 语句?

  • 如何使用正则表达式查询名称?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    授予用户对所有表的访问权限

    • 5 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve