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 ...
随机推荐
- PAT——1024. 科学计数法
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位 ...
- ORACLE 中rownum和row_number()的使用区别(可指定取sql结果集的第几个数据)
这篇文章主要介绍了oracle中rownum和row_number()的使用方法以及区别和联系,十分的详细,有需要的小伙伴可以参考下. row_number()over(partition by ...
- lwip 2.0.3 DNS 域名解析 使用
1. 在 lwipopts.h 中 #define LWIP_DNS 1 /* 使能 DNS 服务器的功能 ,2018年1月8日21:16:20,suozhang */ #define LWIP_ ...
- Selenium报错整理
1. driver不匹配(常见于打不开浏览器,或者浏览器能打开但是获取不了网页元素,或者无法sendKey等问题) Exception in thread "main" org.o ...
- Oracle 体系结构五
确定实例是否是RAC数据库的一部分:select parallel from v$instance; 确定数据库是否通过Data Guard备用数据库的保护来防止数据丢失:select protect ...
- 闲来无事做了一个项目,内有redis,EasyUI样式简单应用,七层分页查询,API跨域。
<link href="~/jquery-easyui-1.5.3/themes/default/easyui.css" rel="stylesheet" ...
- ABAP术语-V1 Module
V1 Module 原文;http://www.cnblogs.com/qiangsheng/archive/2008/03/21/1115707.html Function module creat ...
- Windows 安装 MySQL 8.0.11
下载并解压 从官方网站下载最新安装包 解压到目标安装目录 新建配置文件 在安装目录新建my.ini文件 添加如下内容(需修改为自己的配置) #----------------------------- ...
- bat脚本实现复制特定后缀文件到其他目录
@echo off for /r %%a in (*.txt) do copy %%a D:\1 pause 1.for /r主要用于搜索指定路径及其所有子目录中符合要求的文件(/r后如果没有指定目录 ...
- react 使用antd 按需加载
使用 react-app-rewired 1. 安装react-app-rewired: 由于新的 react-app-rewired@2.x 版本的关系,你还需要安装 customize-cra. ...