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 ...
随机推荐
- cocos2d-x3.0 用CCDictionary写文件
bool CDownLoad_LocalData::WriteToConfigFile( DownLoadLocalData* downdata ){ CCDictionary* pDict = CC ...
- Java监听器原理及实例
一.监听器原理 监听器是基于事件驱动的,用于对操作事件进行监听.处理,是观察者设计模式的应用 监听器三元素: 2.1 事件源:事件发生的源头 2.2 事件:对事件进行抽象.封装 2.3 监听器:用于监 ...
- python 文件上传本地服务器
1:python之上传文件 1.1.url代码 """untitled1222 URL Configuration The `urlpatterns` list rout ...
- node引入bootstrap npm报错
今天node引入bootstrap npm报错 但是页面正常显示 最后发现bootstrap.min.js.map没有放在文件里 虽然不用页面中引入 另外也发现了怎么看这种错误了
- Python编程Message: CGI script is not executable ('/cgi-bin/xxxxx.py')
Message: CGI script is not executable ('/cgi-bin/xxxxx.py'). 今天在练习python服务器端编程时遇到了这个错误,查阅一番最终解决 系统为l ...
- ionic2添加支付宝插件出现问题
安装本地路径插件正常 编译正常 在打开支付页面时候 就报这个错 在手机app点击无效 错误信息: ERROR Error: Uncaught (in promise): Error: No pro ...
- jQuery之修改li下样式和图片
<script type="text/javascript"> $(document).ready(function(){ $('li').click(function ...
- 利用node中的内置模块fs实现对简单文件的读取 拷贝 创建等功能
1.文件的读取 我们想要根据如下一种目录生成一种json数据 代码如下 //此函苏是对目录进行读取的 //我们想要生成的是一个根据目录所创建的json数据 const fs = require(&qu ...
- php 计算两个日期相差天数
<?php $startdate=strtotime("2013-3-09"); $enddate=strtotime("2013-4-05"); $da ...
- 【转】ruby rake执行rspec
RSpec 是Ruby的一个行为驱动开发(BDD)工具,当前的版本是 2.10.根据其入门文档,安装好之后,可以使用 rspec 命令来运行“测试”.但在某些情况下,如果参数较多,使用该命令并不方便: ...