Oracle insert all 详解

Oracle insert all 详解文章目录1概述2insert的两种形式2.1insertfirst2.2insertall3数据一致性(同时插入)2.1验证:insertinto数据不一致2.2验证:insertall数据一致1概述1.作用:’正确、高效’的将’同一批数据’插入至’不同的表’中2.好处(1)’正确’:避免数据差异(2)’高效’:优于写多个insertinto(因为无论插入多少张表,’主表’只会被读取一次)3.场景,若需求:将表t中

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1 概述

1. 作用:'正确、高效' 的将 '同一批数据' 插入至 '不同的表'2. 好处
   (1) '正确':避免数据差异
   (2) '高效':优于写多个 insert into(因为无论插入多少张表,'主表' 只会被读取一次)
   
3. 场景,若需求:将表 t 中的数据 '同时插入' 至表 t1、t2
   若不知晓 insert all 语句,咱可能会使用 insert into 两次
   insert into t1 select * from t;
   insert into t2 select * from t;

   问题:在两次 insert 过程中,有可能 t 表的数据发生了改变,
   从而导致 t1、t2 '得到的数据不一致'。
   
   解决办法:insert all

2 insert 的两种形式

1. insert first: 仅对 '第一个' 匹配成功项进行插入
2. insert all  : 对 '每个' 匹配成功项都进行插入

基础数据准备:

create table stu_info (
   sno   number(3),
   sname varchar2(30),
   sex   varchar2(2)
);

insert into stu_info(sno, sname, sex) values(1, '瑶瑶', '女');
insert into stu_info(sno, sname, sex) values(2, '优优', '男');
insert into stu_info(sno, sname, sex) values(3, '倩倩', '女');
commit;

-- 两张测试表
create table stu_info_1 as select * from stu_info where 1 = 2;
create table stu_info_2 as select * from stu_info where 1 = 2;

2.1 insert first

-- 仅对 '第一个' 匹配成功项进行插入
insert first
  when sno >= 2 then -- 不能用别名哦,如:t.sno
    into stu_info_1(sno, sname, sex)
  when sno >= 3 then
    into stu_info_2(sno, sname, sex) 
select t.sno, t.sname, t.sex from stu_info t;

查询结果:

select * from stu_info;
select * from stu_info_1;
select * from stu_info_2;

图示:仅对 ‘第一个’ 匹配成功项进行插入
在这里插入图片描述

2.2 insert all

-- 对 '每个' 匹配成功项都进行插入
insert all
  when sno >= 2 then -- 不能写别名哦,如:t.sno
    into stu_info_1(sno, sname, sex)
  when sno >= 3 then
    into stu_info_2(sno, sname, sex) 
select t.sno, t.sname, t.sex from stu_info t;

查询结果:对 ‘每个’ 匹配成功项都进行插入
在这里插入图片描述

3 数据一致性(同时插入)

3.1 验证:insert into 数据不一致

1. 模拟:将表 stu_info 中的数据同时插入 stu_info_1 和 stu_info_2

2. 分三个窗口模拟 '同时插入(并行)'
   (1) 窗口1: 将 stu_info 数据插入 stu_info_1(模拟时长 30 s)
   (2) 窗口2: 将 stu_info 数据插入 stu_info_2(模拟时长 30 s)
   (3) 窗口3:此时更新 stu_info 记录,使之影响 tu_info_1 和 stu_info_2(上述模拟时长内) 

3. 清空表 stu_info_1、stu_info_2 -- 若有数据
   truncate table stu_info_1;
   truncate table stu_info_2;

4. dbms_lock 包权限 -- 若无权限,sys 用户授权
   -- conn system/system@orcl as sysdba
   grant execute on sys.dbms_lock to scott;

图示:
在这里插入图片描述

窗口1:插入 stu_info_1,更新 sno = 2 的记录时,等待(模拟执行时长)

declare
begin
  for i in 1 .. 3 loop
  
    if i = 2 then
      dbms_lock.sleep(30); -- 模拟执行时长:30 秒
    end if;
  
    insert into stu_info_1
      (sno, sname, sex)
      select t.sno, t.sname, t.sex from stu_info t where t.sno = i;
    commit;
  
  end loop;
end;

窗口2:插入 stu_info_1,更新 sno = 3 的记录时,等待(模拟执行时长)

declare
begin
  for i in 1 .. 3 loop
  
    if i = 3 then
      dbms_lock.sleep(30); -- 模拟执行时长:30 秒
    end if;
  
    insert into stu_info_2
      (sno, sname, sex)
      select t.sno, t.sname, t.sex from stu_info t where t.sno = i;
    commit;
  
  end loop;
end;

窗口3:更新 stu_info 记录,使之影响 tu_info_1 和 stu_info_2(上述模拟时长内)

update stu_info t set t.sname = 'update_2', t.sex = '22' where t.sno = 2;
commit;

测试结果:stu_info_1 和 stu_info_2 两者记录不一致!
在这里插入图片描述

3.2 验证:insert all 数据一致

1. 同理,可分为 两个窗口测试

2. 清空表 stu_info_1、stu_info_2,并还原 stu_info 的数据

窗口1:插入数据至 stu_info_1 和 stu_info_1

declare
begin
   for i in 1 .. 3 loop
   
      if i = 2 then
         dbms_lock.sleep(30); -- 模拟执行时长:30 秒
      end if;
   
      insert all 
         into stu_info_1(sno, sname, sex) 
         into stu_info_2(sno, sname, sex)
      select t.sno, t.sname, t.sex from stu_info t where t.sno = i; 
      commit;
   
   end loop;
end;

窗口2:更新 stu_info 记录,使之影响 tu_info_1 和 stu_info_2(上述模拟时长内)

update stu_info t set t.sname = 'update_3', t.sex = '33' where t.sno = 3;
commit;

测试结果:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160934.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • windows杀进程命令_win11开机进入安全模式

    windows杀进程命令_win11开机进入安全模式1、查找占用某端口号的进程netstat-aon|findstr89992、杀之taskkill-f-pid进程号

    2022年9月5日
    3
  • 群体智能优化算法

    群体智能优化算法群体智能优化算法介绍什么是群体智能优化算法群体智能优化算法的定义群体智能优化算法的原则常见的群体智能优化算法挖坑蚁群算法粒子群优化算法菌群算法蛙跳算法人工蜂群算法总结由于在研究生期间一直研究粒子群优化算法(ParticleSwarmOptimization),所以对其他的一些群体智能优化算法(SwarmIntelligence)也是有一定的了解的,既然自己在这方面还算有一些自己的见解,所以…

    2022年5月10日
    71
  • MFC学习(24)线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法

    MFC学习(24)线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法注:使用结构CRITICAL_SECTION需加入头文件#include“afxmt.h”定义一个全局的锁CRITICAL_SECTION的实例和一个静态全局变量CRITICAL_SECTION cs;//可以理解为锁定一个资源static int n_

    2022年9月17日
    0
  • 15个你可能不知道的开源云平台

    15个你可能不知道的开源云平台1.1云服务环境Eucalyptus1.1.1介绍ElasticUtilityComputingArchitectureforLinkingYourProgramsToUsef

    2022年7月2日
    18
  • 取消li圆点_css去掉li前面的点

    取消li圆点_css去掉li前面的点在ul标签上写样式是在ul而不是li

    2022年10月24日
    0
  • POJ 2996 Help Me with the Game (模拟)

    POJ 2996 Help Me with the Game (模拟)题目链接:http://poj.org/problem?id=2996POJ训练计划中的模拟都是非常棒的模拟,也非常有代表性。这个题讲的是给你一个国际象棋棋盘,敲代码打印出黑白两方的棋子。以及棋子的坐标。可是须要注意的国际棋盘的坐标问题例如以下图这个国际棋盘能够看到数字轴和字母轴的方向以及增减关系。所以在这个题的统计的时候须要进行坐标转换。由于已经做过类似的方法…

    2022年8月12日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号