oracle第二天
Posted zhaoshuzhan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle第二天相关的知识,希望对你有一定的参考价值。
集合运算
SQL> host cls
SQL> /*
SQL> 查询部门号是10和20的员工信息
SQL> 1. select * from emp where deptno in (10,20);
SQL> 2. select * from emp where deptno=10 or deptno=20;
SQL> 3. 集合运算
SQL> select * from emp where deptno=10
SQL> 加上
SQL> select * from emp where deptno=20;
SQL> */
SQL> select * from emp where deptno=10
2 union
3 select * from emp where deptno=20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 9400
29025
已选择13行。
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,sum(sal) from emp group by deptno
4 union
5 select sum(sal) from emp;
select deptno,sum(sal) from emp group by deptno
*
第 3 行出现错误:
ORA-01789: 查询块具有不正确的结果列数
SQL> /*
SQL> 注意的问题
SQL> 1. 参与运算的各个集合必须列数相同 且类型一致
SQL> 2. 采用第一个集合的表头作为最后的表头
SQL> 3. 如果排序,必须 在每个集合后使用相同order by
SQL> 4. 括号
SQL> */
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 9400
29025
已选择13行。
SQL> break on deptno skip 2
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750
20 ANALYST 6000
CLERK 1900
MANAGER 2975
10875
DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 CLERK 950
MANAGER 2850
SALESMAN 5600
9400
29025
已选择13行。
SQL> break on null
SQL> host cls
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 9400
29025
已选择13行。
SQL> set timing on
SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 9400
29025
已选择13行。
已用时间: 00: 00: 00.00
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 9400
29025
已选择13行。
已用时间: 00: 00: 00.01
SQL> --优化6: 尽量不要使用集合运算
SQL> set timing off
SQL> host cls
SQL> --交集
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 INTERSECT
4 select ename,sal from emp
5 where sal between 1201 and 1400;
ENAME SAL
---------- -----
MARTIN 1250
MILLER 1300
WARD 1250
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 minus
4 select ename,sal from emp
5 where sal between 1201 and 1400;
ENAME SAL
---------- -----
ADAMS 1100
JAMES 950
SMITH 800
SQL> spool off
解锁和改密码
1. 管理员登录
c:\>sqlplus sys/你的密码 as sysdba (密码认证)
c:\>sqlplus / as sysdba (主机认证 优先)
2. 解锁
SQL>alter user scott account unlock;
3. 改密码
SQL>alter user scott identified by 新密码;
课堂练习
SQL> -- rownum 行号 伪列
SQL> select rownum, empno,ename,sal
2 from emp;
ROWNUM EMPNO ENAME SAL
---------- ---------- ---------- -----
1 7369 SMITH 800
2 7499 ALLEN 1600
3 7521 WARD 1250
4 7566 JONES 2975
5 7654 MARTIN 1250
6 7698 BLAKE 2850
7 7782 CLARK 2450
8 7788 SCOTT 3000
9 7839 KING 5000
10 7844 TURNER 1500
11 7876 ADAMS 1100
ROWNUM EMPNO ENAME SAL
---------- ---------- ---------- -----
12 7900 JAMES 950
13 7902 FORD 3000
14 7934 MILLER 1300
已选择14行。
SQL> select rownum,empno,ename,sal
2 from emp
3 where rownum<=3
4 order by sal desc;
ROWNUM EMPNO ENAME SAL
---------- ---------- ---------- -----
2 7499 ALLEN 1600
3 7521 WARD 1250
1 7369 SMITH 800
SQL> /*
SQL> 关于行号
SQL> 1. 按照默认的顺序生成
SQL> 2. rownum只能使用< <=, 不能> >=*/
SQL> select rownum, empno,ename,sal
2 from emp
3 order by sal desc;
ROWNUM EMPNO ENAME SAL
---------- ---------- ---------- -----
9 7839 KING 5000
13 7902 FORD 3000
8 7788 SCOTT 3000
4 7566 JONES 2975
6 7698 BLAKE 2850
7 7782 CLARK 2450
2 7499 ALLEN 1600
10 7844 TURNER 1500
14 7934 MILLER 1300
3 7521 WARD 1250
5 7654 MARTIN 1250
ROWNUM EMPNO ENAME SAL
---------- ---------- ---------- -----
11 7876 ADAMS 1100
12 7900 JAMES 950
1 7369 SMITH 800
已选择14行。
SQL> host cls
SQL> --第一题
SQL> select rownum,empno,ename,sal
2 from (select * from emp order by sal desc)
3 where rownum<=3;
ROWNUM EMPNO ENAME SAL
---------- ---------- ---------- -----
1 7839 KING 5000
2 7788 SCOTT 3000
3 7902 FORD 3000
SQL> select rownum,empno,ename,sal
2 from emp
3 where rownum>=5 and rownum<=8;
未选定行
SQL> select *
2 from (select rownum r,e1.*
3 from (select * from emp order by sal) e1
4 where rownum <=8
5 )
6 where r >=5;
R EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- ---------- --------- ---------- -------------- ----- ---------- ----------
5 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
6 7934 MILLER CLERK 7782 23-1月 -82 1300 10
7 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
8 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
SQL> host cls
SQL> --第二题
SQL> select e.empno,e.ename,e.sal,d.avgsal
2 from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
3 where e.deptno=d.deptno and e.sal>d.avgsal;
EMPNO ENAME SAL AVGSAL
---------- ---------- ----- ----------
7698 BLAKE 2850 1566.66667
7499 ALLEN 1600 1566.66667
7902 FORD 3000 2175
7788 SCOTT 3000 2175
7566 JONES 2975 2175
7839 KING 5000 2916.66667
已选择6行。
SQL> --相关子查询: 将主查询中的某个值 作为参数传递给子查询
SQL> select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
2 from emp e
3 where sal> (select avg(sal) from emp where deptno=e.deptno);
EMPNO ENAME SAL AVGSAL
---------- ---------- ----- ----------
7499 ALLEN 1600 1566.66667
7566 JONES 2975 2175
7698 BLAKE 2850 1566.66667
7788 SCOTT 3000 2175
7839 KING 5000 2916.66667
7902 FORD 3000 2175
已选择6行。
SQL> host cls
SQL> --第三题
SQL> select hiredate from emp;
HIREDATE
--------------
17-12月-80
20-2月 -81
22-2月 -81
02-4月 -81
28-9月 -81
01-5月 -81
09-6月 -81
13-7月 -87
17-11月-81
08-9月 -81
13-7月 -87
HIREDATE
--------------
03-12月-81
03-12月-81
23-1月 -82
已选择14行。
SQL> /*
SQL> HIREDATE count81=0
SQL> ------------------------
SQL> 17-12月-80 0
SQL> 20-2月 -81 1
SQL> 22-2月 -81 1
SQL> 02-4月 -81 1
SQL> 28-9月 -81 1
SQL> 01-5月 -81 1
SQL> 09-6月 -81 1
SQL> 13-7月 -87 0
SQL> 17-11月-81 1
SQL> 08-9月 -81 1
SQL> 13-7月 -87 0
SQL> 03-12月-81 1
SQL> 03-12月-81 1
SQL> 23-1月 -82 0
SQL> ===========================
SQL> 10
SQL>
SQL> sum(if 是81年 then +1 else +0)
SQL> */
SQL> host cls
SQL> --组函数 行转列 wm_concat
SQL> select deptno,wm_concat(ename) names
2 from emp
3 group by deptno;
DEPTNO
----------
NAMES
------------------------------------------------------------------------------------------------------------------------
10
CLARK,KING,MILLER
20
SMITH,FORD,ADAMS,SCOTT,JONES
30
ALLEN,BLAKE,MARTIN,TURNER,JAMES,WARD
SQL> col names for a40
SQL> select deptno,wm_concat(ename) names
2 from emp
3 group by deptno;
DEPTNO NAMES
---------- ----------------------------------------
10 CLARK,KING,MILLER
20 SMITH,FORD,ADAMS,SCOTT,JONES
30 ALLEN,BLAKE,MARTIN,TURNER,JAMES,WARD
SQL> spool off
子查询
SQL> host cls
SQL> --查询工资比SCOTT高的员工信息
SQL> --1. SCOTT的工资
SQL> select sal from emp where ename=‘SCOTT‘;
SAL
----------
3000
SQL> --2.比3000高的
SQL> set linesize 120
SQL> col sal for 9999
SQL> select * from emp where sal > 3000;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
SQL> --解决的问题:不能一步求解
SQL> select *
2 from emp
3 where sal > (select sal
4 from emp
5 where ename=‘SCOTT‘);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
SQL> /*
SQL> 注意的问题
SQL> 1. 括号
SQL> 2. 合理的书写风格
SQL> 3. 可以主查询的where select from having后面放置子查询
SQL> 4. 不可以在主查询的group by后面放置子查询
SQL> 5. 强调from后面的子查询
SQL> 6. 主查询和子查询可以不是同一张表,只要子查询返回的结果 主查询可以使用即可
SQL> 7. 一般不在子查询使用order by,但在Top-N分析问题中 必须使用order by
SQL> 8. 一般先执行子查询,再执行主查询;但相关子查询除外
SQL> 9. 单行子查询只能使用单行操作符 多行子查询只能使用多行操作符
SQL> 10. 子查询中null
SQL> */
SQL> -- 3. 可以主查询的where select from having后面放置子查询
SQL> select ename,sal,(select job from emp where empno=7839) 一列
2 from emp;
ENAME SAL 一列
---------- ----- ---------
SMITH 800 PRESIDENT
ALLEN 1600 PRESIDENT
WARD 1250 PRESIDENT
JONES 2975 PRESIDENT
MARTIN 1250 PRESIDENT
BLAKE 2850 PRESIDENT
CLARK 2450 PRESIDENT
SCOTT 3000 PRESIDENT
KING 5000 PRESIDENT
TURNER 1500 PRESIDENT
ADAMS 1100 PRESIDENT
ENAME SAL 一列
---------- ----- ---------
JAMES 950 PRESIDENT
FORD 3000 PRESIDENT
MILLER 1300 PRESIDENT
已选择14行。
SQL> --5. 强调from后面的子查询
SQL> --查询员工的姓名和薪水
SQL> select *
2 from (select ename,sal from emp);
ENAME SAL
---------- -----
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500
ADAMS 1100
ENAME SAL
---------- -----
JAMES 950
FORD 3000
MILLER 1300
已选择14行。
SQL> --查询员工的姓名 薪水 年薪
SQL> ed
已写入 file afiedt.buf
1 select *
2* from (select ename,sal,sal*12 annlsal from emp)
SQL> /
ENAME SAL ANNLSAL
---------- ----- ----------
SMITH 800 9600
ALLEN 1600 19200
WARD 1250 15000
JONES 2975 35700
MARTIN 1250 15000
BLAKE 2850 34200
CLARK 2450 29400
SCOTT 3000 36000
KING 5000 60000
TURNER 1500 18000
ADAMS 1100 13200
ENAME SAL ANNLSAL
---------- ----- ----------
JAMES 950 11400
FORD 3000 36000
MILLER 1300 15600
已选择14行。
SQL> --6. 主查询和子查询可以不是同一张表,只要子查询返回的结果 主查询可以使用即可
SQL> --查询部门名称为SALES的员工信息
SQL> select *
2 from emp
3 where deptno=(select deptno
4 from dept
5 where dname=‘SALES‘);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
已选择6行。
SQL> select e.*
2 from emp e,dept d
3 where e.deptno=d.deptno and d.dname=‘SALES‘;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
已选择6行。
SQL> --优化5:理论上,尽量使用多表查询
SQL> host cls
SQL> --in 在集合中
SQL> --查询部门名称是SALES和ACCOUNTING的员工
SQL> select *
2 from emp
3 where deptno in (select deptno from dept where dname=‘SALES‘ or dname=‘ACCOUNTING‘);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择9行。
SQL> select e.*
2 from emp e,dept d
3 where e.deptno=d.deptno and (d.dname=‘SALES‘ or d.dname=‘ACCOUNTING‘);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择9行。
SQL> --any: 和集合中 任意一个值比较
SQL> --查询工资比30号部门任意一个员工高的员工信息
SQL> select *\
2 ;
select *\
*
第 1 行出现错误:
ORA-00911: 无效字符
SQL> select *
2 from emp
3 where sal > any (select sal from emp where deptno=30);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
已选择12行。
SQL> ed
已写入 file afiedt.buf
1 select *
2 from emp
3* where sal > (select min(sal) from emp where deptno=30)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择12行。
SQL> --all:和集合的所有值比较
SQL> --查询工资比30号部门所有员工高的员工信息
SQL> select *
2 from emp
3 where sal > all (select sal from emp where deptno=30);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
SQL> ed
已写入 file afiedt.buf
1 select *
2 from emp
3* where sal > (select max(sal) from emp where deptno=30)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
SQL> host cls
SQL> --多行子查询中null
SQL> --查询不是老板的员工信息
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择14行。
SQL> --是老板的员工
SQL> select *
2 from emp
3 where empno in (select mgr from emp);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81 3000 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7839 KING PRESIDENT 17-11月-81 5000 10
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已选择6行。
SQL> ed
已写入 file afiedt.buf
1 select *
2 from emp
3* where empno not in (select mgr from emp)
SQL> /
未选定行
SQL> ed
已写入 file afiedt.buf
1 select *
2 from emp
3* where empno not in (select mgr from emp where mgr is not null)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7369 SMITH CLERK 7902 17-12月-80 800 20
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
已选择8行。
SQL> spool off
处理数据
SQL> host cls
SQL> select count(*) Total,
2 sum(decode(to_char(hiredate,‘RR‘),‘80‘,1,0)) "1980",
3 sum(decode(to_char(hiredate,‘RR‘),‘81‘,1,0)) "1981",
4 sum(decode(to_char(hiredate,‘RR‘),‘82‘,1,0)) "1982",
5 sum(decode(to_char(hiredate,‘RR‘),‘87‘,1,0)) "1987"
6 from emp;
TOTAL 1980 1981 1982 1987
---------- ---------- ---------- ---------- ----------
14 1 10 1 2
SQL> host cls
SQL> /*
SQL> SQL类型
SQL> 1. DML(Data Manipulation Lanuage 数据操作语言): select insert update delete
SQL> 2. DDL(Data Definition Language 数据定义语言): create/alter/drop/truncate table
SQL> create/drop view/sequence/index/synonym
SQL> 3. DCL(Data Control Language 数据控制语言): commit rollback
SQL> */
SQL> host cls
SQL> --插入数据 insert
SQL> insert into emp(empno,ename,sal ,deptno)
2 values(1001,‘Tom‘,6000,20);
已创建 1 行。
SQL> --隐式/显式插入null
SQL> --地址符 &
SQL> insert into emp(empno,ename,sal,deptno)
2 values(&empno,&ename,&sal,&deptno);
输入 empno 的值: 1002
输入 ename 的值: ‘Mary‘
输入 sal 的值: 5000
输入 deptno 的值: 10
原值 2: values(&empno,&ename,&sal,&deptno)
新值 2: values(1002,‘Mary‘,5000,10)
已创建 1 行。
SQL> /
输入 empno 的值: 1003
输入 ename 的值: ‘Mike‘
输入 sal 的值: 6000
输入 deptno 的值: 30
原值 2: values(&empno,&ename,&sal,&deptno)
新值 2: values(1003,‘Mike‘,6000,30)
已创建 1 行。
SQL> ed
已写入 file afiedt.buf
1 insert into emp(empno,ename,sal,deptno)
2* values(&empno,‘&ename‘,&sal,&deptno)
SQL> /
输入 empno 的值: 1004
输入 ename 的值:
以上是关于oracle第二天的主要内容,如果未能解决你的问题,请参考以下文章