ORACLE循序渐进讲解(第二讲)
一、ORACLE常用数据类型(其他类型不太常用)
二、建表
SQL> create table student( 2 xh number(4), 3 xm varchar2(20), 4 sex char(2), 5 birthday date, 6 sal number(7,2) 7 ); Table created
SQL> create table class( 2 classid number(2), 3 cname varchar2(20) 4 ); Table created
SQL> alter table student add (classid number(2)); Table altered
2、修改字段的长度。修改学生表xm长度为30
SQL> alter table student modify (xm varchar2(30)); Table altered
SQL> alter table student modify(xm char(30)); Table altered
SQL> alter table student rename column xm to mc; Table altered
4、删除一个字段。删除学生表sal字段。
SQL> alter table student drop column sal; Table altered
5、修改表名。修改student为stu。
SQL> rename student to stu; Table renamed
6、删除表。删除stu表。
SQL> drop table stu; Table dropped
三、表基本查询(使用scott用户)。
SQL> desc emp; Name Type Nullable Default Comments -------- ------------ -------- ------- -------- EMPNO NUMBER(4) ENAME VARCHAR2(10) Y JOB VARCHAR2(9) Y MGR NUMBER(4) Y HIREDATE DATE Y SAL NUMBER(7,2) Y COMM NUMBER(7,2) Y DEPTNO NUMBER(2) Y
2、查询每个雇员的年工资。
SQL> select ename "姓名",sal*12 "年工资" from emp; 姓名 年工资 ---------- ---------- SMITH 21600 ALLEN 19200 WARD 15000 JONES 35700 MARTIN 15000 BLAKE 34200 CLARK 29400 SCOTT 36000 KING 60000 TURNER 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 15600 14 rows selected注:用nvl将null转化为0
SQL> select ename "姓名",sal*12+nvl(comm,0)*12 "年工资" from emp; 姓名 年工资 ---------- ---------- SMITH 21600 ALLEN 22800 WARD 21000 JONES 35700 MARTIN 31800 BLAKE 34200 CLARK 29400 SCOTT 36000 KING 60000 TURNER 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 16014.72 14 rows selected
3、如何显示第三个字符为大写O的所有员工的姓名和工资。
SQL> select ename,sal from emp where ename like '__O%'; ENAME SAL ---------- --------- SCOTT 3000.00
注:%表示任意0到多个字符,_表示任意单个字符。
4、显示每个部门的平均工资和最高工资。
SQL> select avg(sal) "平均工资",max(sal) "最高工资",deptno "部门编号" from emp group by deptno; 平均工资 最高工资 部门编号 ---------- ---------- -------- 1566.66666 2850 30 2375 3000 20 2916.66666 5000 10
5、显示每个部门的每种岗位的平均工资和最低工资。
SQL> select avg(sal) "平均工资",min(sal) "最低工资",deptno,job from emp group by deptno,job order by deptno; 平均工资 最低工资 DEPTNO JOB ---------- ---------- ------ --------- 1300 1300 10 CLERK 2450 2450 10 MANAGER 5000 5000 10 PRESIDENT 3000 3000 20 ANALYST 1450 1100 20 CLERK 2975 2975 20 MANAGER 950 950 30 CLERK 2850 2850 30 MANAGER 1400 1250 30 SALESMAN 9 rows selected
6、显示平均工资低于2000的部门号和他的平均工资。
SQL> select deptno,avg(sal) "平均工资" from emp group by deptno having avg(sal)<2000; DEPTNO 平均工资 ------ ---------- 30 1566.66666
7、显示和SMITH同一部门的所有员工。
SQL> select ename from emp where deptno=(select deptno from emp where ename='SMITH'); ENAME ---------- SMITH JONES SCOTT ADAMS FORD
8、查询和部门10的工作相同的雇员的名字、岗位、工资、部门号
SQL> select ename,job,sal,deptno from emp where job in (select job from emp where deptno=10); ENAME JOB SAL DEPTNO ---------- --------- --------- ------ CLARK MANAGER 2450.00 10 BLAKE MANAGER 2850.00 30 JONES MANAGER 2975.00 20 KING PRESIDENT 5000.00 10 MILLER CLERK 1300.00 10 JAMES CLERK 950.00 30 ADAMS CLERK 1100.00 20 SMITH CLERK 1800.00 20 8 rows selected
9、显示工资比部门30的所有员工的工资高的员工姓名、工资和部门号。
SQL> select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30); ENAME SAL DEPTNO ---------- --------- ------ JONES 2975.00 20 SCOTT 3000.00 20 KING 5000.00 10 FORD 3000.00 20或者
SQL> select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30); ENAME SAL DEPTNO ---------- --------- ------ JONES 2975.00 20 SCOTT 3000.00 20 KING 5000.00 10 FORD 3000.00 20
10、查询与SMITH部门和岗位完全相同的所有雇员
SQL> select ename from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH'); ENAME ---------- SMITH ADAMS
11、查询高于自己部门的平均工资的员工信息。
SQL> select ename,sal,svg "部门平均工资" from emp,(select deptno,avg(sal) svg from emp group by deptno) t where sal>svg and emp.deptno=t.deptno; ENAME SAL 部门平均工资 ---------- --------- ------------ ALLEN 1600.00 1566.6666666 JONES 2975.00 2375 BLAKE 2850.00 1566.6666666 SCOTT 3000.00 2375 KING 5000.00 2916.6666666 FORD 3000.00 2375 6 rows selected
SQL> update emp set (job,sal,comm)=(select job,sal,comm from emp where ename='SMITH') where ename='SCOTT'; 1 row updated
13、oracle分页。
SQL> select * from emp where empno in(select empno from (select empno,rownum rn from (select empno from emp order by sal desc) where rownum<=10) t where t.rn>=6); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ----- ----------- --------- --------- ------ 7782 CLARK MANAGER 7839 1981-06-09 2450.00 10 7844 TURNER SALESMAN 7698 1981-09-08 1500.00 0.00 30 7934 MILLER CLERK 7782 1982-01-23 1300.00 34.56 10 7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 300.00 30 7369 SMITH CLERK 7902 1980-12-17 1800.00 20
14、用查询结果创建新表
SQL> create table emp1(id,name) as select empno,ename from emp; Table created Executed in 0.532 seconds SQL> select * from emp1; ID NAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected
15、合并查询。(union、union all、intersect、minus)
SQL> select empno id,ename name from emp 2 union 3 select id,name from emp1; ID NAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected
(2)union all 取两个结果集的并集,不会去掉结果集中重复行。
SQL> select empno id,ename name from emp 2 union all 3 select id,name from emp1 4 ; ID NAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE ID NAME ----- ---------- 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 28 rows selected
(3)intersect 取两个结果的交集。
SQL> select ename,sal from emp where sal>2500 2 intersect 3 select ename,sal from emp where job='MANAGER'; ENAME SAL ---------- --------- BLAKE 2850.00 JONES 2975.00
SQL> select ename,sal from emp where sal>2500 2 minus 3 select ename,sal from emp where job='MANAGER' 4 ; ENAME SAL ---------- --------- FORD 3000.00 KING 5000.00 SCOTT 3000.00
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。