Oracle集合
--union 并集
select * from emp where ename like '%A%' union
select * from emp where ename like '%M%';
--union all 集并 公共部分 会包含二次
select * from emp where ename like '%A%'
union all
select * from emp where ename like '%M%';
--intersert交集
select * from emp where ename like '%A%'
intersect
select * from emp where ename like '%M%';
--minus求差集 S1: A - (S1&S2 union)1100-1200
select * from emp where sal between 700 and 1200 --700-1100;
minus
select * from emp where sal between 1100 and 1500; --联合与全联合运算
--union
create table emp_history as select * from emp ;--辅助 select empno,ename,sal,hiredate,deptno
from emp where deptno =20
union
select empno,ename,sal,hiredate,deptno
from emp_history where deptno =30
order by deptno; -- union all 不能消除重复行,不能输出排序 使用DISTINCT关键字
select empno,ename,sal,hiredate,deptno
from emp where deptno =20
union all
select empno,ename,sal,hiredate,deptno
from emp_history where deptno =30
order by deptno; --相交运算 1.列数和数据类型与select语句一样,但列名可以不同 select empno,ename,sal,hiredate,deptno from emp
where deptno =20 intersect
select empno,ename,sal,hiredate,deptno from emp_history
where deptno=20; --相减运算
--查询在第一个表中而不再第二个表中的行
select empno,ename,sal,hiredate,deptno from emp
where deptno=20
minus
select empno,ename,sal,hiredate,deptno from emp
where deptno=20;
--结构化查询 实现递归表的查询
select * from emp;
select level,lpad(' ',2*(level-1))||ename, empno,mgr,hiredate,sal from emp
start with mgr is null --start with以manager_id is null作为跟节点
connect by prior empno=mgr;
--根据connect by prior规则,继续向下寻找,形成树状结构查询 --用子查询插入数据
create table emp_copy as select * from emp where 1=2;
insert into emp_copy select * from emp where deptno=20;
--insert插入多表数据
create table emp_dept_10 as select * from emp where 1=2;
create table emp_dept_20 as select * from emp where 1=2;
create table emp_dept_30 as select * from emp where 1=2;
insert first
when deptno =10
then
into emp_dept_10
when deptno=20
then
into emp_dept_20
when deptno=30
then
into emp_dept_30
else
into emp_copy
select * from emp; select * from emp_dept_10;
--Merge语句
merge into emp_copy c --目标表
using emp e on (c.empno=e.empno) --源表,可以是表,视图或查询
when MATCHED then update --当匹配时,进行update操作
set c.ename=e.ename,c.job=e.job,c.mgr=e.mgr,
c.hiredate=e.hiredate,c.sal=e.sal,c.comm=e.comm,
c.deptno =e.deptno
when not matched then
insert
values(e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
e.deptno); create table dept60_bonuses
(
empno number,bonus_amt number
);
insert into dept60_bonuses values(7369,0);
insert into dept60_bonuses values(7788,2);
insert into dept60_bonuses values(7876,3);
select empno,sal,ename from emp;
select * from dept60_bonuses;
--合并两张表,根据不同的语句删除,更新
merge into dept60_bonuses b
using ( select empno,sal,deptno from emp where deptno=20) e
on (b.empno=e.empno)
when matched then
update set b.bonus_amt =e.sal*0.2
where b.bonus_amt=0
delete where (e.sal>2500)
when not matched then
insert (b.empno,b.bonus_amt)
values (e.empno,e.sal*0.1)
where (e.sal<4000);
使用TRUNCATE清除表数据
与delete语句相比,使用truncate命令速度更快,原因 DTL
1不会激活表的删除触发器
2 属于数据定义语言,不会产生撤销信息
3 主外键关系无法清除表内容,必须禁用约束
不能使用PLSQL语句块 直接调用
--禁用约束
alter table dept disable constraint pk_dept cascade;
提交 commit; 回滚 rollback;
--sql>
create table jobs (adds varchar2(10),jname varchar2(20),sal number(10),comm number(10));
delete from jobs;
insert into jobs values('OFFICE','办公文员',3000,5000);
savepoint sp;
insert into jobs values('FINANCE','财务人员',4000,8000);
select * from jobs;
rollback to savepoint sp;
使用集合方法
--exits 方法 集合的坐标元素是否存在
declare
type projectlist is varray (50) of varchar2(16);
project_list projectlist:=projectlist('网站','ERP','CRM','CMS');
begin
if project_list.exists(5)
then
dbms_output.put_line('元素存在,其值为:'||project_list(5));
else
dbms_output.put_line('元素不存在');
end if;
end;
Oracle集合的更多相关文章
- Oracle集合操作函数:union、intersect、minus
[转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...
- oracle 集合变量以及自定义异常的用法
oracle 集合变量以及自定义异常的用法, 在过程 record_practice 有record变量和自定义异常的用法实例.具体在3284行. CREATE OR REPLACE Package ...
- Oracle集合类型
Oracle集合类型介绍 集合类型 1. 使用条件: a. 单行单列的数据,使用标量变量 . b. 单行多列数据,使用记录 c. 单列多行数据,使用集合 *集 ...
- oracle 集合定义
集合:是具有相同定义的元素的聚合.Oracle有两种类型的集合: 可变长数组(VARRAY):可以有任意数量的元素,但必须预先定义限制值. 嵌套表:视为表中之表,可以有任意数量的元素,不需要预先定义限 ...
- Oracle集合操作
在Oracle中提供了三种类型的集合操作: 并(UNION).交(INTERSECT).差(MINUS) UNION:将多个查询的结果组合到一个查询结果之中,并去掉反复值 UNION ALL:将多个查 ...
- Oracle 集合操作
在 Oracle 中提供了三种类型集合操作:并(UNION).交(INTERSECT).差(MINUS) · UNION:将多个查询的结果组合到一个查询结果之中,没有重复内容 · UNION ALL: ...
- 【database】oracle集合 - Associative Arrays、Varrays、Nested Tables
前言 参考oracle官方文档:PL/SQL Language Reference 11g Release 2 - 5 PL/SQL Collections and Records 可以去看下文档 ...
- 【转】Oracle集合操作函数:union、intersect、minus
集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...
- Oracle集合运算符 交集 并集 差集
集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集 一.union求并集,公共部分只有包含一次 例:求emp表ename中含’A‘或含有‘M’ SQL&g ...
- oracle 集合运算符
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY4AAACNCAIAAAAvhQoxAAAbmklEQVR4nO1dX6jc1pn/0lBH4KVV6J ...
随机推荐
- C语言的谜题
本篇文章<C语言的谜题>展示了14个C语言的迷题以及答案,代码应该是足够清楚的,而且我也相信有相当的一些例子可能是我们日常工作可能会见得到的.通过这些迷题,希望你能更了解C语言.如果你不看 ...
- 如何求出数组中最小(或者最大)的k个数(least k问题)
输入n个整数,如何求出其中最小的k个数? 解法1. 当然最直观的思路是将数组排序,然后就可以找出其中最小的k个数了,时间复杂度以快速排序为例,是O(nlogn): 解法2. 借助划分(Partitio ...
- 阿里云阿里免费ssl wap网站在手机微信、手机浏览器无法访问
图片可以访问,样式无法显示 https://css.cnbuses.com/css/wap/gonggong.css 1,怀疑是开了代理访问. 关闭后还是访问空白. 2.在手机浏览器打开,提示该网站的 ...
- sqlplus连接半天才连上
问题现象: 某oracle数据库服务器发现使用ssh,crt连接半天1-2分钟后才返回输入密码的提示,应用人员发现使用 sys_GUID()函数获取唯一值的时候,第一次调用需要等待很长时间,但是同一s ...
- (Les16 执行数据库恢复)-重做日志文件恢复
丢失重做日志文件 丢失了重做日志文件组中的某个成员,并且组中至少还有一个成员: -不会影响实例的正常操作. -预警日志中会收到一条信息, ...
- c++学习笔记(新手学习笔记,如有错误请与作者联系)
逗号”,“运算符:a = 公式1,公式2:把公式1的结果放进公式2中进行运算,如: a = 3*5 , a*4; 计算结果:a = 3*5*4=60; typedef:类型别名,为已有类型另外命名 t ...
- zookeeper报错 JAVA_HOME is not set
很多开发者安装zookeeper的时候,应该会发现到这么一个问题: JAVA_HOME is not set 好的!那么这个是什么意思呢? 就是说你的 JAVA_HOME 变量没有设定 为什么会提示 ...
- 14JavaScript条件语句
条件语句用于基于不同的条件来执行不同的动作. 1.条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用以 ...
- webstorm累计
websorm官网下载安装: 1.一下粗略截图说明,点击下一步下一步安装到合适的路径下. 2.next下一步下一步安装成功后弹出,点击ok就行. 2.再次运行webstorm快捷方式打开:界面如下: ...
- CDH部署(以5.7.5为例)
博客园首发,转载请注明出处https://www.cnblogs.com/tzxxh/p/9120020.html 一.准备工作(下面的内容括号内写master的表示仅在master节点执行,all代 ...