• Oracle 多表插入


    多表插入
    作用:一条INSERT语句可以完成向多张表的插入任务(Multitable insert)。有两种形式:insert all与insert first,
    准备测试环境:
    1.创建表T并初始化测试数据,此表作为数据源。
    create table t (x number(10), y varchar2(10));
    insert into t values (1,'a');
    insert into t values (2,'b');
    insert into t values (3,'c');
    insert into t values (4,'d');
    insert into t values (5,'e');
    insert into t values (6,'f');
    commit;

    2.创建表T1和T2,作为我们要插入数据的目标表。
    SQL>create table t1 as select * from t where 0=1;
    SQL>create table t2 as select * from t where 0=1;

    第一种多表插入方法INSERT ALL
    SQL>insert all into t1 into t2 select * from t; (无条件insert all)
    12 rows created.
    这里之所以显示插入了12条数据,实际上表示在T1表中插入了6条,T2表插入了6条,一共是12条数据。(不分先后,各插各的)
    SQL>rollback;
    SQL> insert all when x>=3 then into t1 when x>=2 then into t2 select * from t; (有条件insert all)。

    ALL是,默认值,数据库评估每个条件时不管任何其他条件评估的结果。为每个条件的求值结果为true时,数据库执行相应的插入。

    第二种多表插入方法INSERT FIRST
    1)清空表T1和T2
    SQL> truncate table t1;
    SQL> truncate table t2;
    2)完成INSERT FIRST插入
    SQL> insert first when x>=3 then into t1 when x>=2 then into t2 select * from t; (有条件insert first)
    处理逻辑是这样的,首先检索T表查找X列值大于等于3的数据插入到T1表,然后将前一个查询中出现的数据排除后再查找T表,找到X列值大于等于2的数据再插入到T2表,注意INSERT FIRST的真正目的是将同样的数据只插入一次。
    3)验证T1和T2表中被插入的数据。
    SQL> select * from t1;
    SQL> select * from t2;

  • 相关阅读:
    NOIP2008双栈排序[二分图染色|栈|DP]
    洛谷P1108 低价购买[DP | LIS方案数]
    洛谷P1330封锁阳光大学[二分图染色]
    NOIP模板整理计划
    期中考试
    UVA 10564 Paths through the Hourglass[DP 打印]
    UVA 11404 Palindromic Subsequence[DP LCS 打印]
    POJ2479 Maximum sum[DP|最大子段和]
    POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
    UVA11324 The Largest Clique[强连通分量 缩点 DP]
  • 原文地址:https://www.cnblogs.com/zhaochunyi/p/10867733.html
Copyright © 2020-2023  润新知