--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集合的更多相关文章

  1. Oracle集合操作函数:union、intersect、minus

    [转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...

  2. oracle 集合变量以及自定义异常的用法

    oracle 集合变量以及自定义异常的用法, 在过程 record_practice 有record变量和自定义异常的用法实例.具体在3284行. CREATE OR REPLACE Package ...

  3. Oracle集合类型

    Oracle集合类型介绍   集合类型   1. 使用条件:    a. 单行单列的数据,使用标量变量 .     b. 单行多列数据,使用记录    c. 单列多行数据,使用集合        *集 ...

  4. oracle 集合定义

    集合:是具有相同定义的元素的聚合.Oracle有两种类型的集合: 可变长数组(VARRAY):可以有任意数量的元素,但必须预先定义限制值. 嵌套表:视为表中之表,可以有任意数量的元素,不需要预先定义限 ...

  5. Oracle集合操作

    在Oracle中提供了三种类型的集合操作: 并(UNION).交(INTERSECT).差(MINUS) UNION:将多个查询的结果组合到一个查询结果之中,并去掉反复值 UNION ALL:将多个查 ...

  6. Oracle 集合操作

    在 Oracle 中提供了三种类型集合操作:并(UNION).交(INTERSECT).差(MINUS) · UNION:将多个查询的结果组合到一个查询结果之中,没有重复内容 · UNION ALL: ...

  7. 【database】oracle集合 - Associative Arrays、Varrays、Nested Tables

    前言 参考oracle官方文档:PL/SQL Language Reference 11g Release 2  -  5 PL/SQL Collections and Records 可以去看下文档 ...

  8. 【转】Oracle集合操作函数:union、intersect、minus

    集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...

  9. Oracle集合运算符 交集 并集 差集

     集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集  一.union求并集,公共部分只有包含一次 例:求emp表ename中含’A‘或含有‘M’ SQL&g ...

  10. oracle 集合运算符

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY4AAACNCAIAAAAvhQoxAAAbmklEQVR4nO1dX6jc1pn/0lBH4KVV6J ...

随机推荐

  1. Python 学习笔记(十四)Python类(二)

    创建简单的类 新式类和经典类(旧式类) Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Python 3.x中默认都是新式类,经典类被移除,不必显式的继承object 新式 ...

  2. Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.UserService' that could not be found

    Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.Us ...

  3. 【leetcode】867 - Transpose Matrix

    [题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...

  4. React简单实现双向数据绑定

    import React, { Component } from 'react' import ReactDOM from 'react-dom' class App extends Componen ...

  5. Windows 安装 MySQL 8.0.11

    下载并解压 从官方网站下载最新安装包 解压到目标安装目录 新建配置文件 在安装目录新建my.ini文件 添加如下内容(需修改为自己的配置) #----------------------------- ...

  6. CentOS 7 Minimal 安装JDK 1.8

    真好最近比较闲,打算在linux 的CentOS 7 Minimal版本试着搭建hadoop环境学习学习,当然第一步就是在CentOS 7 Minimal 安装JDK 1.8环境.其实老早就打算了解一 ...

  7. 面试被问到IIC,总结。

    Linux3.5内核中,IIC. i2c_add_driver i2c_register_driver a. at24cxx_driver放入i2c_bus_type的drv链表 并且从dev链表里取 ...

  8. 【python安装】Windows上安装和创建python开发环境

    1. 在 windows10 上安装python开发环境 Linux和Mac OS都自带python环境,但是Windows没有,所以需要自行安装. 第1步:访问 python官网,下载Windows ...

  9. git提交代码速度过慢解决方法(macos)

    iterm2中ping下github.com和github.global.ssl.fastly.net $ ping github.com 得到以下 PING github.com (192.30.2 ...

  10. C语言顺序队列

    顺序队列是一种只能在一头进和另一头出的数据结构,所以结构体里设2个指针分别指向头部和尾部,用数组来存储数据. #define MAXSIZE 1024 typedef int elemtype; ty ...