Oracle基本操作练习(一)

  1 --创建表空间
  2 create tablespace test
  3 datafile 'c:\test.dbf'
  4 size 100m
  5 autoextend on
  6 next 10m;
  7 --删除表空间
  8 drop tablespace test;
  9 
 10 --创建用户
 11 create user test
 12 identified by test
 13 default tablespace test;
 14 
 15 --给用户授权
 16 --oracle数据库中常用角色
 17 connect--连接角色,基本角色
 18 resource--开发者角色
 19 dba--超级管理员角色
 20 --给test用户授予dba角色
 21 grant dba to test;
 22 
 23 ---切换到test用户下
 24 
 25 ---创建一个person表
 26 create table person(
 27        pid number(20),
 28        pname varchar2(10)
 29 );
 30 
 31 ---修改表结构
 32 ---添加一列
 33 alter table person add (gender number(1));
 34 ---修改列类型
 35 alter table person modify gender char(1);
 36 ---修改列名称
 37 alter table person rename column gender to sex;
 38 ---删除一列
 39 alter table person drop column sex;
 40 
 41 ---查询表中记录
 42 select * from person;
 43 ----添加一条记录
 44 insert into person (pid, pname) values (1, '小明');
 45 commit;
 46 ----修改一条记录
 47 update person set pname = '小马' where pid = 1;
 48 commit;
 49 
 50 ----三个删除
 51 --删除表中全部记录
 52 delete from person;
 53 --删除表结构
 54 drop table person;
 55 --先删除表,再次创建表。效果等同于删除表中全部记录。
 56 --在数据量大的情况下,尤其在表中带有索引的情况下,该操作效率高。
 57 --索引可以提供查询效率,但是会影响增删改效率。
 58 truncate table person;
 59 
 60 
 61 ----序列不真的属于任何一张表,但是可以逻辑和表做绑定。
 62 ----序列:默认从1开始,依次递增,主要用来给主键赋值使用。
 63 ----dual:虚表,只是为了补全语法,没有任何意义。
 64 create sequence s_person;
 65 select s_person.nextval from dual;
 66 ----添加一条记录
 67 insert into person (pid, pname) values (s_person.nextval, '小明');
 68 commit;
 69 select * from person;
 70 
 71 ----scott用户,密码tiger。
 72 --解锁scott用户
 73 alter user scott account unlock;
 74 --解锁scott用户的密码【此句也可以用来重置密码】
 75 alter user scott identified by tiger;
 76 --切换到scott用户下
 77 
 78 --单行函数:作用于一行,返回一个值。
 79 ---字符函数
 80 select upper('yes') from dual;--YES
 81 select lower('YES') from dual;--yes
 82 ----数值函数
 83 select round(56.16, -2) from dual;---四舍五入,后面的参数表示保留的位数
 84 select trunc(56.16, -1) from dual;---直接截取,不在看后面位数的数字是否大于5.
 85 select mod(10, 3) from dual;---求余数
 86 ----日期函数
 87 ----查询出emp表中所有员工入职距离现在几天。
 88 select sysdate-e.hiredate from emp e;
 89 ----算出明天此刻
 90 select sysdate+1 from dual;
 91 ----查询出emp表中所有员工入职距离现在几月。
 92 select months_between(sysdate,e.hiredate) from emp e;
 93 ----查询出emp表中所有员工入职距离现在几年。
 94 select months_between(sysdate,e.hiredate)/12 from emp e;
 95 ----查询出emp表中所有员工入职距离现在几周。
 96 select round((sysdate-e.hiredate)/7) from emp e;
 97 ----转换函数
 98 ---日期转字符串
 99 select to_char(sysdate, 'fm yyyy-mm-dd hh24:mi:ss') from dual;
100 ---字符串转日期
101 select to_date('2018-6-7 16:39:50', 'fm yyyy-mm-dd hh24:mi:ss') from dual;
102 ----通用函数
103 ---算出emp表中所有员工的年薪
104 ----奖金里面有null值,如果null值和任意数字做算术运算,结果都是null。
105 select e.sal*12+nvl(e.comm, 0) from emp e;
106 
107 ---条件表达式
108 ---条件表达式的通用写法,mysql和oracle通用
109 ---给emp表中员工起中文名
110 select e.ename, 
111        case e.ename
112          when 'SMITH' then '曹贼'
113            when 'ALLEN' then '大耳贼'
114              when 'WARD' then '诸葛小儿'
115                --else '无名'
116                  end
117 from emp e;
118 ---判断emp表中员工工资,如果高于3000显示高收入,如果高于1500低于3000显示中等收入,
119 -----其余显示低收入
120 select e.sal, 
121        case 
122          when e.sal>3000 then '高收入'
123            when e.sal>1500 then '中等收入'
124                else '低收入'
125                  end
126 from emp e;
127 ----oracle中除了起别名,都用单引号。
128 ----oracle专用条件表达式
129 select e.ename, 
130         decode(e.ename,
131           'SMITH',  '曹贼',
132             'ALLEN',  '大耳贼',
133               'WARD',  '诸葛小儿',
134                 '无名') "中文名"             
135 from emp e;
136 
137 --多行函数【聚合函数】:作用于多行,返回一个值。
138 select count(1) from emp;---查询总数量
139 select sum(sal) from emp;---工资总和
140 select max(sal) from emp;---最大工资
141 select min(sal) from emp;---最低工资
142 select avg(sal) from emp;---平均工资
143 
144 
145 ---分组查询
146 ---查询出每个部门的平均工资
147 ---分组查询中,出现在group by后面的原始列,才能出现在select后面
148 ---没有出现在group by后面的列,想在select后面,必须加上聚合函数。
149 ---聚合函数有一个特性,可以把多行记录变成一个值。
150 select e.deptno, avg(e.sal)--, e.ename
151 from emp e
152 group by e.deptno;
153 ---查询出平均工资高于2000的部门信息
154 select e.deptno, avg(e.sal) asal
155 from emp e
156 group by e.deptno
157 having avg(e.sal)>2000;
158 ---所有条件都不能使用别名来判断。
159 --比如下面的条件语句也不能使用别名当条件
160 select ename, sal s from emp where sal>1500;
161 
162 ---查询出每个部门工资高于800的员工的平均工资
163 select e.deptno, avg(e.sal) asal
164 from emp e
165 where e.sal>800
166 group by e.deptno;
167 ----where是过滤分组前的数据,having是过滤分组后的数据。
168 ---表现形式:where必须在group by之前,having是在group by之后。
169 ---查询出每个部门工资高于800的员工的平均工资
170 ---然后再查询出平均工资高于2000的部门
171 select e.deptno, avg(e.sal) asal
172 from emp e
173 where e.sal>800
174 group by e.deptno
175 having avg(e.sal)>2000;
176 
177 
178 ---多表查询中的一些概念
179 ---笛卡尔积
180 select *
181 from emp e, dept d;
182 ---等值连接
183 select *
184 from emp e, dept d
185 where e.deptno=d.deptno;
186 ---内连接
187 select *
188 from emp e inner join dept d
189 on e.deptno = d.deptno;
190 ---查询出所有部门,以及部门下的员工信息。【外连接】
191 select *
192 from emp e right join dept d
193 on e.deptno=d.deptno;
194 ---查询所有员工信息,以及员工所属部门
195 select *
196 from emp e left join dept d
197 on e.deptno=d.deptno;
198 ---oracle中专用外连接
199 select *
200 from emp e, dept d
201 where e.deptno(+) = d.deptno;
202 
203 select * from emp;
204 ---查询出员工姓名,员工领导姓名
205 ---自连接:自连接其实就是站在不同的角度把一张表看成多张表。
206 select e1.ename, e2.ename
207 from emp e1, emp e2
208 where e1.mgr = e2.empno;
209 ------查询出员工姓名,员工部门名称,员工领导姓名,员工领导部门名称
210 select e1.ename, d1.dname, e2.ename, d2.dname
211 from emp e1, emp e2, dept d1, dept d2
212 where e1.mgr = e2.empno
213 and e1.deptno=d1.deptno
214 and e2.deptno=d2.deptno;
215 
216 ---子查询
217 ---子查询返回一个值
218 ---查询出工资和SCOTT一样的员工信息
219 select * from emp where sal in
220 (select sal from emp where ename = 'SCOTT')
221 ---子查询返回一个集合
222 ---查询出工资和10号部门任意员工一样的员工信息
223 select * from emp where sal in
224 (select sal from emp where deptno = 10);
225 ---子查询返回一张表
226 ---查询出每个部门最低工资,和最低工资员工姓名,和该员工所在部门名称
227 ---1,先查询出每个部门最低工资
228 select deptno, min(sal) msal
229 from emp 
230 group by deptno;
231 ---2,三表联查,得到最终结果。
232 select t.deptno, t.msal, e.ename, d.dname
233 from (select deptno, min(sal) msal
234       from emp 
235       group by deptno) t, emp e, dept d
236 where t.deptno = e.deptno
237 and t.msal = e.sal
238 and e.deptno = d.deptno;
239 
240 
241 ----oracle中的分页
242 ---rownum行号:当我们做select操作的时候,
243 --每查询出一行记录,就会在该行上加上一个行号,
244 --行号从1开始,依次递增,不能跳着走。
245 
246 ----排序操作会影响rownum的顺序
247 select rownum, e.* from emp e order by e.sal desc
248 ----如果涉及到排序,但是还要使用rownum的话,我们可以再次嵌套查询。
249 select rownum, t.* from(
250 select rownum, e.* from emp e order by e.sal desc) t;
251 
252 
253 ----emp表工资倒叙排列后,每页五条记录,查询第二页。
254 ----rownum行号不能写上大于一个正数。
255 select * from(
256     select rownum rn, tt.* from(
257           select * from emp order by sal desc
258     ) tt where rownum<11
259 ) where rn>5

猜你喜欢

转载自www.cnblogs.com/yscec/p/12046369.html