一、连接查询
1.内连接
内连接用于返回满足连接条件的所有记录。默认情况下,在执行连接查询时如果没有指定任何连接操作符,那么这些连接查询都属于内连接。
- SELECT a.dname,b.ename from dept a,emp b where a.deptno=b.deptno and a.deptno=10;
- SELECT a.dname,b.ename from dept a,emp b where a.deptno=b.deptno and a.deptno=10;
SELECT a.dname,b.ename from dept a,emp b where a.deptno=b.deptno and a.deptno=10;
在FROM子句中指定INNER JOIN选项,也可以指定内连接。
- SELECT a.dname,b.ename from dept a INNER JOIN emp b where a.deptno=b.deptno and a.deptno=10;
- SELECT a.dname,b.ename from dept a INNER JOIN emp b where a.deptno=b.deptno and a.deptno=10;
SELECT a.dname,b.ename from dept a INNER JOIN emp b where a.deptno=b.deptno and a.deptno=10;
从Oracle9i开始,如果主表的主键列和从表的外部键列名称相同,那么可以使用NATURAL JOIN 关键字自动执行内连接操作。
- SELECT dname,ename FROM dept NATURAL JOIN emp;
- SELECT dname,ename FROM dept NATURAL JOIN emp;
SELECT dname,ename FROM dept NATURAL JOIN emp;
2.左外连接
左外连接通过指定LEFT[OUTER] JOIN选项来实现。当使用左外连接时,不仅会返回满足连接条件的所有记录,而且还会返回不满足连接条件的连接操作符左边表的其他行。
- SELECT a.dname,b.ename FROM dept a LEFT JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
- SELECT a.dname,b.ename FROM dept a LEFT JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
SELECT a.dname,b.ename FROM dept a LEFT JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
3.右外连接
右外连接通过指定RIGHT[OUTER] JOIN选项来实现的。当使用右外连接时,不仅会返回满足连接条件的所有行,而且还会返回不满足连接条件操作符右边表的其他行。
- SELECT a.dname,b.ename FROM dept a RIGHT JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
- SELECT a.dname,b.ename FROM dept a RIGHT JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
SELECT a.dname,b.ename FROM dept a RIGHT JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
4.完全外连接
完全外连接是指定FULL[OUTER] JOIN 选项来实现的。当使用完全外连接时,不仅会返回满足连接条件的所有行,而且还会返回不满足连接条件的所有其他行。
- SELECT a.dname,b.ename FROM dept a FULL JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
- SELECT a.dname,b.ename FROM dept a FULL JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
SELECT a.dname,b.ename FROM dept a FULL JOIN emp b ON a.deptno=b.deptno AND a.deptno=10;
5.使用(+)操作符
在Oracle9i之前,当执行外连接时,都是使用连接操作符(+)来完成的。尽管可以使用操作符(+)执行外连接操作。但Oralce9i开始Oracle建议使用OUTER JOIN执行外连接。语法如下:
SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1(+) = table2.column2;
当使用(+)操作符执行外连接时,应该将该操作符放在显示较少行(完全满足连接条件行)的一端。
一句话,左外连接时,(+)要放在右边表的一端。右外连接时,(+)放在左边表的一端。
左外连接:
- SELECT a.dname,b.ename FROM dept a, emp b where a.deptno=b.deptno(+) AND b.deptno(+)=10;
- SELECT a.dname,b.ename FROM dept a, emp b where a.deptno=b.deptno(+) AND b.deptno(+)=10;
SELECT a.dname,b.ename FROM dept a, emp b where a.deptno=b.deptno(+) AND b.deptno(+)=10;
右外连接
- SELECT a.dname,b.ename FROM dept a, emp b where a.deptno(+)=b.deptno AND a.deptno(+)=10;
- SELECT a.dname,b.ename FROM dept a, emp b where a.deptno(+)=b.deptno AND a.deptno(+)=10;
SELECT a.dname,b.ename FROM dept a, emp b where a.deptno(+)=b.deptno AND a.deptno(+)=10;
二、递归查询
1.语法
- Select * from …. Where [结果过滤条件语句]
- Start with [起始条件过滤语句]
- Connect by prior [中间记录过滤条件语句]
- Select * from …. Where [结果过滤条件语句]
- Start with [起始条件过滤语句]
- Connect by prior [中间记录过滤条件语句]
Select * from …. Where [结果过滤条件语句]
Start with [起始条件过滤语句]
Connect by prior [中间记录过滤条件语句]
2.例子
- Select * from company t Where t.flag=1
- Start with t.company_id=50500000
- Connect by prior t.company_id=t.parent_id
- Select * from company t Where t.flag=1
- Start with t.company_id=50500000
- Connect by prior t.company_id=t.parent_id
Select * from company t Where t.flag=1
Start with t.company_id=50500000
Connect by prior t.company_id=t.parent_id
说明:
- select [level],column,expr from table [where condition]
- [start with] //[起点]
- [connect by prior + 主键=外键 或 外键=主键]
- select [level],column,expr from table [where condition]
- [start with] //[起点]
- [connect by prior + 主键=外键 或 外键=主键]
select [level],column,expr from table [where condition]
[start with] //[起点]
[connect by prior + 主键=外键 或 外键=主键]
a.自顶向下: 左边放主键,右边放外键。
b.自底向上: 右边放主键,左边放外键。
c.level(伪列)层次的级别,不固定值。
三、更改数据库的时间格式
1、临时更改
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'
2.永久更改
在注册表[HKEY_CODE_MACHINE\SOFTWARE\ORACLE]里面,加入NLS_DATE_FORMAT,值为YYYY-MM-DD HH24:MI:SS。
3、Unix下,在用户的.profile文件中增加以下内容:
NLS_DATE_FORMAT=YYYY-MM-DD HH24:MI:SS
export NLS_DATE_FORMAT
附:在SQLPLUS下,实现中-英字符集转换
- alter session set nls_language='AMERICAN';
- alter session set nls_language='SIMPLIFIED CHINESE';
- alter session set nls_language='AMERICAN';
- alter session set nls_language='SIMPLIFIED CHINESE';
alter session set nls_language='AMERICAN';
alter session set nls_language='SIMPLIFIED CHINESE';
四、复制表和数据
1)建表
- create table test as select * from dept;
- create table test as select * from dept where 1=2;
- create table test as select * from dept;
- create table test as select * from dept where 1=2;
create table test as select * from dept; --从已知表复制数据和结构
create table test as select * from dept where 1=2; --从已知表复制结构但不包括数据
建立视图:
- CREATE OR REPLACE VIEW dept_10 AS SELECT empno,ename,job,sal,deptno FROM emp WHERE deptno=10 ORDER BY empno;
- CREATE OR REPLACE VIEW dept_10 AS SELECT empno,ename,job,sal,deptno FROM emp WHERE deptno=10 ORDER BY empno;
CREATE OR REPLACE VIEW dept_10 AS SELECT empno,ename,job,sal,deptno FROM emp WHERE deptno=10 ORDER BY empno;
建立实体化视图:
- CREATE MATERIALLIZED VIEW summary_emp AS SELECT deptno,job,avg(sal) avgsal, sum(sal) sumsal FROM emp GROUP BY cube(deptno,job);
- CREATE MATERIALLIZED VIEW summary_emp AS SELECT deptno,job,avg(sal) avgsal, sum(sal) sumsal FROM emp GROUP BY cube(deptno,job);
CREATE MATERIALLIZED VIEW summary_emp AS SELECT deptno,job,avg(sal) avgsal, sum(sal) sumsal FROM emp GROUP BY cube(deptno,job);
实体化视图管理是用于汇总,预计算,复制或分发数据的对象, 在大型的数据库中使用它可以提高涉及到的SUM,COUNT,AVG,MIN,MAX等的表的查询的速度,只要在实体化视图管理上创建了统计,查询优化器将自动的使用实体化视图管理,这特性被称为QUERY REWRITE(查询重写).与普通的视图不同的是实体化视图管理存储数据,占据数据库的物理空间的.
2)不使用列的列表插入数据:
- insert into test select * from dept;
- insert into test select * from dept;
insert into test select * from dept;
3)使用DEFAULT插入数据:如果列存在默认值,则会使用其默认值;如果列不存在默认值,则自动使用NULL.
- insert into dept values(10,'市场部',default);
- insert into dept values(10,'市场部',default);
insert into dept values(10,'市场部',default);
4)使用子查询插入数据
- insert into employee(empno,ename,sql,deptno)
- select empno,ename,sal,deptno from emp
- where deptno = 10;
- insert into employee(empno,ename,sql,deptno)
- select empno,ename,sal,deptno from emp
- where deptno = 10;
insert into employee(empno,ename,sql,deptno)
select empno,ename,sal,deptno from emp
where deptno = 10;
当要装载大批量数据时,用直接装载方式:
- insert /*+APPEND */ into employee(empno,ename,sql,deptno)
- select empno,ename,sal,deptno from emp
- where deptno = 10;
- insert /*+APPEND */ into employee(empno,ename,sql,deptno)
- select empno,ename,sal,deptno from emp
- where deptno = 10;
insert /*+APPEND */ into employee(empno,ename,sql,deptno)
select empno,ename,sal,deptno from emp
where deptno = 10;
5)使用多表插入数据。从Oracle9i开始,使用INSERT语句可以将某张表的数据同时插入到多张表中。
使用ALL操作符,在每个条件子句上都要执行INTO子句后的子查询。
- insert all when deptno=10 then into t_dept10
- when deptno=20 then into t_dept20
- when deptno=30 then into t_dept30
- when job='clerk' then into t_clerk
- else into t_other
- select * from emp;
- insert all when deptno=10 then into t_dept10
- when deptno=20 then into t_dept20
- when deptno=30 then into t_dept30
- when job='clerk' then into t_clerk
- else into t_other
- select * from emp;
insert all when deptno=10 then into t_dept10
when deptno=20 then into t_dept20
when deptno=30 then into t_dept30
when job='clerk' then into t_clerk
else into t_other
select * from emp;
使用FIRST操作符执行多表插入,如果数据已经满足了先前条件,并且已经被插入到某表,那么该行数据在后续插入中将不会被再次使用。
- insert first when deptno=10 then into t_dept10
- when deptno=20 then into t_dept20
- when deptno=30 then into t_dept30
- when job='clerk' then into t_clerk
- else into t_other
- select * from emp;
- insert first when deptno=10 then into t_dept10
- when deptno=20 then into t_dept20
- when deptno=30 then into t_dept30
- when job='clerk' then into t_clerk
- else into t_other
- select * from emp;
insert first when deptno=10 then into t_dept10
when deptno=20 then into t_dept20
when deptno=30 then into t_dept30
when job='clerk' then into t_clerk
else into t_other
select * from emp;
五、多列子查询
多列子查询是指返回多列数据的子查询语句。当多列子查询返回单行数据时,在WHERE子句中可以使用单行比较符;当多列子查询返回多行数据时,在WHERE子句中必须使用多行比较符(IN,ANY,ALL)。
如显示与Id为10000的人员部门和岗位完全相同的所有雇员:
- SELECT ename,job,sal,deptno FROM emp WHERE (deptno,job)=(SELECT deptno,job FROM emp WHERE id=10000);
- SELECT ename,job,sal,deptno FROM emp WHERE (deptno,job)=(SELECT deptno,job FROM emp WHERE id=10000);
SELECT ename,job,sal,deptno FROM emp WHERE (deptno,job)=(SELECT deptno,job FROM emp WHERE id=10000);
当执行成对比较是时,因为要求多个列的数据必须同时匹配,所以必须要使用多列子查询实现。
如显示工资和补助与部门30雇员的工资和补助完全匹配的所有雇员:
- SELECT ename,sal,comm,dptno FROM emp WHERE (sal, nvl(comm,-1)) IN (SELECT sal, nvl(comm,-1) FROM emp WHERE deptno=30);
- SELECT ename,sal,comm,dptno FROM emp WHERE (sal, nvl(comm,-1)) IN (SELECT sal, nvl(comm,-1) FROM emp WHERE deptno=30);
SELECT ename,sal,comm,dptno FROM emp WHERE (sal, nvl(comm,-1)) IN (SELECT sal, nvl(comm,-1) FROM emp WHERE deptno=30);
执行非成对比较时,应该使用多个多行子查询来实现。
如显示工资匹配于部门30工资列表、补助匹配于部门30补助列表的所有雇员:
- SELECT ename,sal,comm,deptno FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno=30) AND nvl(comm,-1) IN (SELECT nvl(comm,-1) FROM emp WHERE deptno=30);
- SELECT ename,sal,comm,deptno FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno=30) AND nvl(comm,-1) IN (SELECT nvl(comm,-1) FROM emp WHERE deptno=30);
SELECT ename,sal,comm,deptno FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno=30) AND nvl(comm,-1) IN (SELECT nvl(comm,-1) FROM emp WHERE deptno=30);
六、相关子查询与FROM子句中子查询
相关子查询是指需要引用主查询表列的子查询语句,是通过EXISTS谓词来实现的。
如显示工作在“NEW YORK”的所有雇员:
- SELECT ename,job,sal,deptno FROM emp WHERE EXISTS (SELECT 1 FROM dept WHERE dept.deptno=emp.deptno AND dept.loc='NEW YORK');
- SELECT ename,job,sal,deptno FROM emp WHERE EXISTS (SELECT 1 FROM dept WHERE dept.deptno=emp.deptno AND dept.loc='NEW YORK');
SELECT ename,job,sal,deptno FROM emp WHERE EXISTS (SELECT 1 FROM dept WHERE dept.deptno=emp.deptno AND dept.loc='NEW YORK');
在FROM子句中使用子查询时,该子查询会被作为视图对待。因此也被称为内嵌视图。
如显示高于部门平均工资的雇员信息:
- SELECT ename,job,sal FROM emp, (SELECT deptno,avg(sal) avgsal FROM emp GROUP BY deptno) dept WHERE emp.deptno=dept.deptno AND sal>dept.avgsal;
- SELECT ename,job,sal FROM emp, (SELECT deptno,avg(sal) avgsal FROM emp GROUP BY deptno) dept WHERE emp.deptno=dept.deptno AND sal>dept.avgsal;
SELECT ename,job,sal FROM emp, (SELECT deptno,avg(sal) avgsal FROM emp GROUP BY deptno) dept WHERE emp.deptno=dept.deptno AND sal>dept.avgsal;
七、合并查询
集合操作符有UNION,UNION ALL, INTERSECT和MINUS。当使用集合操作符时,必须确保不同查询的列个数和数据类型都要匹配。
有以下一些限制:
1.对于LOB,VARRAY和嵌套表列来说,集合操作符是无效的。
2.对于LONG列来说,UNION,INTERSECT,MINUS操作符是无效的。
3.如果选择列表包含了表达式,则必须为为其指定列别名。
(一)UNION
UNION操作符用于获取两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。并且会以第一列的结果进行排序。
- SELECT ename,sal,job FROM emp WHERE sal>2500 UNION SELECT ename,sal,job FROM emp WHERE job='MANAGER';
- SELECT ename,sal,job FROM emp WHERE sal>2500 UNION SELECT ename,sal,job FROM emp WHERE job='MANAGER';
SELECT ename,sal,job FROM emp WHERE sal>2500 UNION SELECT ename,sal,job FROM emp WHERE job='MANAGER';
(二)UNION ALL
UNION ALL操作符获取两个结果集的并集,但不会取消重复值,而且也不会以任何列进行排序。
- SELECT ename,sal,job FROM emp WHERE sal>2500 UNION ALL SELECT ename,sal,job FROM emp WHERE job='MANAGER';
- SELECT ename,sal,job FROM emp WHERE sal>2500 UNION ALL SELECT ename,sal,job FROM emp WHERE job='MANAGER';
SELECT ename,sal,job FROM emp WHERE sal>2500 UNION ALL SELECT ename,sal,job FROM emp WHERE job='MANAGER';
(三)INTERSECT
INTERSECT操作符用于获取两个结果集的交集。当使用该操作符时,只会显示同时存在于两个结果集中的数据,并且会以第一列进行排序。
- SELECT ename,sal,job FROM emp WHERE sal>2500 INTERSECT SELECT ename,sal,job FROM emp WHERE job='MANAGER';
- SELECT ename,sal,job FROM emp WHERE sal>2500 INTERSECT SELECT ename,sal,job FROM emp WHERE job='MANAGER';
SELECT ename,sal,job FROM emp WHERE sal>2500 INTERSECT SELECT ename,sal,job FROM emp WHERE job='MANAGER';
(四)MINUS
MINUS用于获取两个结果集的差集。以第一列进行排序。
- SELECT ename,sal,job FROM emp WHERE sal>2500 MINUS SELECT ename,sal,job FROM emp WHERE job='MANAGER';
- SELECT ename,sal,job FROM emp WHERE sal>2500 MINUS SELECT ename,sal,job FROM emp WHERE job='MANAGER';
SELECT ename,sal,job FROM emp WHERE sal>2500 MINUS SELECT ename,sal,job FROM emp WHERE job='MANAGER';
八、CASE表达式和倒叙查询
通过CASE表达式,可以避免调用过程来完成条件分支操作。
- SELECT ename,sal,CASE WHEN sal>3000 THEN 3 WHEN sal>2000 THEN 2 ELSE 1 END grade FROM emp WHERE deptno=10;
- SELECT ename,sal,CASE WHEN sal>3000 THEN 3 WHEN sal>2000 THEN 2 ELSE 1 END grade FROM emp WHERE deptno=10;
SELECT ename,sal,CASE WHEN sal>3000 THEN 3 WHEN sal>2000 THEN 2 ELSE 1 END grade FROM emp WHERE deptno=10;
默认情况下,当执行查询操作时,只能看到最近提交的数据。从Oracle9i开始,通过使用倒叙查询(Flashback Query)特征,可以查看到过去某个时间点所提交的数据。注意,如果使用倒叙查询,那么要示数据库必须彩UNDO管理方式,并且初始化参数undo_retention限制了UNDO数据的保留时间。
- SELECT ename,sal FROM emp AS OF TIMESTAMP to_timestamp('2009-01-01 12:12:00','YYYY-MM-DD HH24:MI:SS') WHERE ename='CLARK';
- SELECT ename,sal FROM emp AS OF TIMESTAMP to_timestamp('2009-01-01 12:12:00','YYYY-MM-DD HH24:MI:SS') WHERE ename='CLARK';
SELECT ename,sal FROM emp AS OF TIMESTAMP to_timestamp('2009-01-01 12:12:00','YYYY-MM-DD HH24:MI:SS') WHERE ename='CLARK';
九、使用WITH子句重用子查询
从Oracle9i开始,通过WITH子句可以给子查询指定一个名称,并且使得在一条语句中可以完成所有任务,从而避免使用临时表。
如显示部门工资总和高于雇员工资总和三分之一的部门名称及工资总和。
- WITH summary AS (SELECT dname,SUM(sal) AS dept_total FROM emp,dept WHERE emp.deptno=dept.deptno GROUP BY dname)
- SELECT dname, dept_total FROM summary WHERE dept_total>(
- SELECT SUM(dept_total) * 1/3 FROM summary);