1 --oracle表的管理
2
3 --创建表
4 create table users(userName varchar2(20));
5 --删除表
6 drop table users;
7 --创建表
8 create table student(xh number(4),xm varchar2(20),sex char(2),birthday date,sal number(7,2));
9 create table classes(classId number(2),cnmae varchar2(40));
10 --添加一个字段
11 alter table student add (classId number(2));
12 --修改字段长度
13 alter table student modify(xm varchar2(30));
14 --修改字段类型/或者名字(不能有数据)
15 alter table student modify(xm char(30));
16 --删除一个字段 不要删字段
17 alter table student drop column sal;
18 --修改表的名字
19 rename student to stu;
20 --删除表
21 drop table student;
22 --oracle中迷人的日期格式'DD-MON-YY' DD代表日,MON代表月 YY代表年
23 --变更日期的输入默认格式 临时生效
24 alter session set nls_date_format='DD-MM-YYYY'
25 --插入空值
26 insert into student(xh,xm,sex,birthday) values(1,'aa','女',null);
27 --查询数据为空的
28 select * from student where birthday is null;
29 --查询数据非空
30 select * from student where birthday is not null;
31 --把所有的男员工的工资减为原来的一半
32 update student set sal=sal/2 where sex='男';
33 --删除数据
34 --删除所有的记录,表结构还在,写日志,还可以恢复,速度慢
35 delect from student;
36 --删除表的结构和数据
37 drop table student;
38 --删除表中的所有记录,表结构还在,不写日志,无法找回删除的记录,速度快
39 truncate table student;
40 --设置保存点
41 savepoint a;
42 --回滚到a点
43 rollback to a;
44 --///////////////////////////////
45 commit
46 --表的查询
47 --查询表结构
48 sesc dept;
49 select * from emp;
50 select * from dept;
51 --疯狂复制
52 create table users(userId varchar2(20),userName varchar2(20),userpasswd varchar2(20));
53 insert into users values('001','aaa','12aaa');
54 insert into users(userId,userName,userpasswd) select * from users;
55 --取消重复行 distinct
56 select distinct deptno,job from emp;
57 --查询SMITH雇员的deptno,job,sal信息
58 select deptno,job,sal from emp where ename='SMITH'
59 --使用算术表达式
60 --显示所有雇员的年工资
61 select ename,sal*13 "年工资" from emp;
62 --加上奖金的
63 --处理null志使用 nvl判断是否为空值
64 --如果查询出comm查询出为空值就用0替代,不为零则使用comm值
65 select ename,sal*13+nvl(comm,0)*13 "年工资" from emp;
66 --显示工资高于3000的员工
67 select * from emp where sal>3000;
68 --查找1982年1.1后入职的员工
69 select ename,hiredate from emp where hiredate>'1-1-1982';
70 --显示工资在2000到2500之间的
71 select * from emp where sal>2000 and sal<2500;
72 --like操作符号
73 --%代表任意的0到多个字符
74 -- _表示任意单个字符
75 --显示首字母是大写字母人的工资
76 select ename ,sal from emp where ename like 'S%'
77 --显示第三个字母为大写O的人的工资
78 select ename ,sal from emp where ename like '__O%'
79 --使用in
80 select * from emp where empno in (7844,234,345);
81 --显示没有上级的人
82 select * from emp where mgr is null;
83
84 --逻辑操作符号
85 select * from emp where (sal>500 or job='MANAGER') and ename like 'J%'
86 --薪水从低到高排
87 select * from emp order by sal;
88 --工资从高到底
89 select * from emp order by sal desc;
90 --按照部门编号排序升序,并按照薪水从高到底(降序)
91 select * from emp order by deptno ,sal desc;
92 --按照部门和升序,入职时间降序
93 select * from emp order by deptno,hiredate ;
94 --使用列的别名排序 按照年薪升序排列
95 select ename,sal*12+nvl(comm,0)*12 "年薪" from emp order by "年薪";
96 ---////////////////////////
97 --表的复杂查询
98 --分组函数和非分组函数不能放在一起
99 --查询出最高工资和最低工资
100 select max(sal),min(sal) from emp;
101 --查询出最高工资的人名
102 select ename,sal from emp where sal =(select max(sal) ab from emp);
103 --显示工资高于平均工资的员工
104 select * from emp where sal<( select avg(sal) from emp);
105 --group by 分组
106 --having 过滤
107 --显示各个部门的平均工资和最高工资
108 select avg(sal),max(sal),min(sal),deptno ,job from emp group by deptno,job;
109 --显示平均工资低于2000的部门号和它的平均工资
110 select avg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000;
111 --总结
112 --分组函数只能出现在选择列表,having,order by子句中
113 --在select中使用的顺序group by>having>order by
114 select avg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000 order by avg(sal) desc;
115 --//////////////////////////////////
116 --多表查询
117 --显示雇员名,雇员工资,及所在部门名称,要用到emp表和dept表
118 select ab.ename ,ab.sal ,ac.dname from emp ab,dept ac where ab.deptno=ac.deptno
119 --如何显示部门号为10的部门名,员工名和工资
120 select a1.ename,a2.dname,a1.sal from emp a1,dept a2 where a1.deptno=a2.deptno and a1.deptno=10
121 --显示雇员名,雇员工资,及所在部门的名字,并按照部门排序
122 select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno order by a1.deptno;
123 --自连接
124 --显示某个员工的上级 'FORD'
125 select worker.ename "员工",boss.ename "上级" from emp worker ,emp boss where worker.mgr=boss.empno and worker.ename='FORD';
126 --子查询
127 --多列子查询
128 ---查询和smith部门号,工作职位一样的员工
129 select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH')
130 --如何显示高于自己部门平均工资的员工的信息
131 --1.各个部门的平均工资
132 select avg(sal) mysal ,deptno from emp group by deptno
133 --2.把上面的查询看做是一张子表
134 select * from emp a2,(select avg(sal) mysal ,deptno from emp group by deptno) a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;
135 ---////////////////////////////////////////
136 --oracle分页,一共有三种
137 --1.rownum分页
138 select * from emp;
139 --2.显示rownum
140 select a1.* ,rownum rn from (select * from emp) a1 ;
141 --3.分页
142 --a,指定查询列,只需改变最里面的
143 --b,排序,也只需修改最里面的查询
144 select a2.* from (select a1.* ,rownum rn from (select ename,sal from emp order by sal) a1 where rownum<9)a2 where rn>=6;
145 --/////////////////////
146 --用查询结果创建一张新表
147 create table myemp2(id,ename,sal)as select empno,ename,sal from emp;
148 desc myemp2;
149 select * from myemp2;
150 --合并查询
151 --为了合并多个select语句的结果,可以使用集合操作符号 union ,union all,minus,intersect
152 --union 改操作符号用于取得两个结果的的并集,当使用该操作符号时会自动去掉结果集合中的重复行
153 --union 改操作符号和union相似,但不会取消重复行,而且不会排序
154 --minus 改操作符号,用于取得两个结果集的差集,只会显示在第一个集合的,而不存在第二个集合中的数据