该文章会分为三部分
1.多表关联批量插入
2.多表关联批量更新
3.多表关联批量删除
首先要明白一点,为什么会有批量这一个概念,无非就是数据太多了,在java端把数据查出来然后在按照100-300的批次进行更新太耗性能了,而且写出来的代码会非常的臃肿,所谓好的实现是用最少的,最精简的代码实现需求,代码越少,留给自己犯错误的机会更少。
还有一个知识点就是多表关联,对于查询肯定是可以多表关联的,其实对于除了查询之外也是可以进行多表关联过滤数据的,从而达到在oracle中查到目标数据即可更新,从而规避规避以往需要单独查一次数据然后在按照100-300的批次做插入,更新,删除的操作 。
创建必须的表和序列语句:
--创建部门表 dept: create table dept ( deptno number(2) constraint pk_dept primary key, dname varchar2(14) , loc varchar2(13) , createdtime date, updatedtime date, createdby number(7,0), updatedby number(7,0) ) ; --创建员工表 emp: create table emp ( empno number(4) constraint pk_emp primary key, ename varchar2(10), job varchar2(15), mgr number(4), hiredate date, sal number(7,2), comm number(7,2), deptno number(2) constraint fk_deptno references dept, createdtime date, updatedtime date, createdby number(7,0), updatedby number(7,0) ); --创建员工表 emp_copy: create table emp_copy ( empno number(4), ename varchar2(10), job varchar2(15), mgr number(4), hiredate date, sal number(7,2), comm number(7,2), deptno number(2), createdtime date, updatedtime date, createdby number(7,0), updatedby number(7,0) ); --自定义一个序列 create sequence emp_sequence increment by 1 --每次增加几个,我这里是每次增加1 start with 1 --从1开始计数 nomaxvalue --不设置最大值 nocycle --一直累加,不循环 nocache --不建缓冲区 --插入dept表数据: insert into dept values(10,'accounting','new york',sysdate,sysdate,123123,123123); insert into dept values (20,'research','dallas',sysdate,sysdate,123123,123123); insert into dept values(30,'sales','chicago',sysdate,sysdate,123123,123123); insert into dept values(40,'operations','boston',sysdate,sysdate,123123,123123); --插入emp表数据: insert into emp values(7369,'smith','clerk',7902,to_date('17-12-1980','dd-mm-yyyy'),800,null,20,sysdate,sysdate,123123,123123); insert into emp values(7499,'allen','salesman',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30,sysdate,sysdate,123123,123123); insert into emp values(7521,'ward','salesman',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30,sysdate,sysdate,123123,123123); insert into emp values(7566,'jones','manager',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,null,20,sysdate,sysdate,123123,123123); insert into emp values(7654,'martin','salesman',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30,sysdate,sysdate,123123,123123); insert into emp values(7698,'blake','manager',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,null,30,sysdate,sysdate,123123,123123); insert into emp values(7782,'clark','manager',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,null,10,sysdate,sysdate,123123,123123); insert into emp values(7788,'scott','analyst',7566,to_date('19-4-87','dd-mm-yyyy'),3000,null,20,sysdate,sysdate,123123,123123); insert into emp values(7839,'king','president',null,to_date('17-11-1981','dd-mm-yyyy'),5000,null,10,sysdate,sysdate,123123,123123); insert into emp values(7844,'turner','salesman',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30,sysdate,sysdate,123123,123123); insert into emp values(7876,'adams','clerk',7788,to_date('23-5-87','dd-mm-yyyy'),1100,null,20,sysdate,sysdate,123123,123123); insert into emp values(7900,'james','clerk',7698,to_date('3-12-1981','dd-mm-yyyy'),950,null,30,sysdate,sysdate,123123,123123); insert into emp values(7902,'ford','analyst',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,null,20,sysdate,sysdate,123123,123123); insert into emp values(7934,'miller','clerk',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,null,10,sysdate,sysdate,123123,123123);
插入数据之后的dept表:
插入数据之后的emp表:
1.多表关联批量插入
要求:批量复制部门dept表 loc在 chicago的,且工资大于1600的人员信息到emp_copy表,emp_copy的empno需要使用序列emp_sequence
insert into emp_copy select emp_sequence.nextval,c.* from ( select e.ename , e.job , e.mgr , e.hiredate, e.sal , e.comm , e.deptno, sysdate as createdtime, sysdate as updatedtime, 123124 as createdby, 123124 as updatedby from emp e, dept d where e.deptno = d.deptno and d.loc='chicago' and e.sal>=1500 )c;
插入之后的emp_copy表结果:
其中select emp_sequence.nextval,c.* 这种格式保证了c.*的多条数据都能取到不同的序列值
2.多表关联批量更新
要求:批量更新部门dept表 loc在 chicago的,且职位job为’salesman’的员工,comm在原来基础上加200
操作之前的数据:
merge into emp e using ( select e1.job, e1.ename,e1.comm from emp e1, dept d1 where e1.deptno = d1.deptno and d1.loc='chicago' and e1.job='salesman' )t on ( e.job = t.job and e.ename = t.ename ) when matched then update set e.comm= t.comm+200
操作之后的数据:
其中e.job = t.job and e.ename = t.ename只是展示可以关联多个条件,这里有一个关键点为 using ( … ) t 中查询到的是一个list,而merge可以对匹配到的list进行批量更新
3.多表关联批量删除
要求:批量删除部门dept表 loc在 chicago的,且工资小于1500的人员信息
操作之前的数据:
delete from( select c.* from emp c,dept d where d.deptno = c.deptno and d.loc = 'chicago' and c.sal < 1500);
操作之后的数据:
总结
以上所述是www.887551.com给大家介绍的oracle中多表关联批量插入批量更新与批量删除操作,希望对大家有所帮助