一、练习题
--1.显示职员的就职年度--使用round()函数将入职日期四舍五入到年份--利用连接操作符将获得的年份和“年度”字符串做拼接select e.ename "姓名",to_char(round(e.hiredate,'yyyy'),'yyyy')||'年度' "入职年度"from emp e;--2.列出至少有三个雇员的所有部门select deptno,count(deptno) from emp group by deptno having count(1)>=3;--3.列出薪金比“SMITH ”多的所有雇员select * from emp;select * from emp e where e.sal>(select e1.sal from emp e1 where e1.ename='SMITH');--4.列出所有“CLERK ”(办事员)的姓名及其部门名称select e.ename , d.dname from emp e,dept d where e.deptno=d.deptno and e.job='CLERK';--5.列出各种工作类别的最低薪金,显示最低薪金大于1500 的记录select min(sal),job from emp group by job;select job,min(sal) from emp group by job having min(sal)>1500;--6.找出各月最后一天受雇的所有雇员select e.* from emp e where last_day(e.hiredate)=e.hiredate;--7.获得当月最后一天函数 last_day(hiredate)select last_day(e.hiredate) from emp;
二、表空间
1、创建表空间
2、查询有哪些表空间
3、调整表空间大小
4、查询数据文件
5、删除表空间
6、查询所有用户信息
--权限不足,需要使用管理员来创建create tablespace ts_42datafile 'd:\file1\ts_42_datafile.dbf'size 5M;--查询有哪些表空间select * from dba_tablespaces;--调整表空间大小--1.直接更改文件大小alter database datafile 'd:\file1\ts_42_datafile.dbf'resize 6M;--2.向表空间内再添加数据文件alter tablespace ts_42add datafile 'd:\file1\ts_42_datafile2.dbf'size 5M;--查询数据文件select * from dba_data_files;--删除表空间 先备份再删除--删除表空间并且删除内容和数据文件drop tablespace ts_42 including contents and datafiles;--查询系统所有用户信息select * from dba_users;
三、创建用户和授权
1、创建用户
2、修改用户密码
3、授权
4、赋予查看其它用户表的权限
5、撤销权限
--创建用户create user user_42identified by 123456;--修改用户密码alter user user_42identified by 123123;--新密码登录前给用户授权grant connect,resource to user_42;--赋予user_42 用户可以查看用户scott中的emp 表的权限grant select on scott.emp to user_42;--grant update on scott.emp to user_42;--登录user_42 用户,查看用户scott的emp 表select * from scott.emp;--给 user_42 撤销权限revoke select on scott.emp from user_42;select * from scott.emp;
四、练习,创建用户,默认表空间和授权
--1.以Sys用户连接Orcl数据库--创建tp_orders表空间大小10Mcreate tablespace tp_ordersdatafile 'd:\file1\tp_orders_datafile.dbf'size 10M;--2.创建 A_oe 用户的默认表空间为tp_orders,密码为bdqncreate user A_oedefault tablespace tp_ordersidentified by bdqn;select * from dba_users;--3.授予connect、resource权限grant connect,resource to A_oe;--4.授予访问scott用户的emp表的权限grant select on scott.emp to A_oe;select * from scott.emp;
五、监测表空间使用情况
--监测表空间使用情况SELECT a.tablespace_name "表空间",Round(a.bytes/(1024*1024*1024),3) "大小G",Round(b.bytes/(1024*1024*1024),3) "已使用G",Round(c.bytes/(1024*1024*1024),3) "剩余G",Round(b.bytes*100/a.bytes,3) "使用百分比%",Round(c.bytes*100/a.bytes,3) "剩余百分比%"FROM sys.Sm$ts_Avail a, sys.Sm$ts_Used b,sys.sm$ts_free cWHERE a.tablespace_name=b.tablespace_name AND a.tablespace_name=c.tablespace_name;
六、创建、访问、修改、删除、使用序列
--创建序列 序号从10 开始,间隔为 2 最大值是30 不循环 缓存5 个create sequence seq1start with 10increment by 2maxvalue 30minvalue 10nocyclecache 5;--使用序列 next 生成下一个值select seq1.nextval from dual;--select seq1.currval from dual;create table toys(id int ,name varchar2(30));select * from toys;insert into toys(id,name) values(seq1.nextval,'A');insert into toys(id,name) values(seq1.nextval,'B');--删除序列--drop sequence seq1;--生成 32 为全局唯一标识select sys_guid() from dual;
七、全局唯一标识
1、oracle中—生成 32 为全局唯一标识 select sys_guid() from dual;
2、java中 public static void main(String[] args) {
System.out.println(UUID.randomUUID().toString().replaceAll(“-“, “”));
八、授权练习
--1.以Sys用户连接Orcl数据库--创建tp_orders表空间大小10Mcreate tablespace tp_ordersdatafile 'd:\file1\tp_orders_datafile.dbf'size 10M;--2.创建 A_oe 用户的默认表空间为tp_orders,密码为bdqncreate user A_oedefault tablespace tp_ordersidentified by bdqn;select * from dba_users;--3.授予connect、resource权限grant connect,resource to A_oe;--4.授予访问scott用户的emp表的权限grant select on scott.emp to A_oe;select * from scott.emp;
九、同义词
两类:私有同义词,共有同义词
私有:只能在当前用户下使用,共有:任何用户均能访问该表
注意:使用同义词前,要获得同义词对应对象的访问权限
最好不要同名,一旦同名,数据库会先选择私有或者对象,再选择共有作为目标
-- user_42 使用私有同义词select * from s_t;-- user_42 删除私有同义词drop synonym s_t;--user_42 创建共有同义词create public synonym s_toys for toys;--sys 授权grant create public synonym to user_42;-- user_42 / scott 使用共有同义词 本用户和其他用户下都可以用select * from s_toys;--登录sys 给 scott授权查询 toys 表达的权限grant select on user_42.toys to scott;--user_42 登录,删除共有同义词drop public synonym s_toys;--sys 授权删除权限grant drop public synonym to user_42;
十、索引的创建和删除
select * from toys;--user42 创建索引create index index_toys_id on toys (id);--user42 删除索引drop index index_toys_id;
索引的作用:唯一性和提高查询速度
反向键索引:单个的索引才能键,减少I/O瓶颈的发生并提高查询速度
位图索引:适用于低基数列,值有限的列。
重建索引:
什么时候应该重建??
1。用户表被移动到新的表空间
2。索引中包含很多已删除的项
3。需要将现有的正常索引转换成反向索引。
十一、表分区
表分区的优势:
1。改善表的查询性能
2。表更容易管理
3。便于备份和恢复
4。提高数据安全性
drop table sales1;--1、范围分区 自己建P1 P2 P3 P4 分区create table sales1(sales_id int,sales_date date)--指定分区partition by range(sales_date)(partition p1 values less than(to_date('2018-12-31','yyyy-mm-dd')),partition p2 values less than(to_date('2019-12-31','yyyy-mm-dd')),partition p3 values less than(to_date('2020-12-31','yyyy-mm-dd')),partition p4 values less than(to_date('2021-12-31','yyyy-mm-dd')),partition p5 values less than(maxvalue));--插入数据insert into sales1( sales_id , sales_date) values(1,to_date('2021-04-01','yyyy-mm-dd'));insert into sales1( sales_id , sales_date) values(2,to_date('2020-04-01','yyyy-mm-dd'));insert into sales1( sales_id , sales_date) values(3,to_date('2019-04-01','yyyy-mm-dd'));insert into sales1( sales_id , sales_date) values(4,to_date('2018-04-01','yyyy-mm-dd'));select * from sales1;--※使用分区表 查询的是2020 整个年度的数据select * from sales1 partition(p3);--2 、间隔分区 应用场景应用更广泛(系统帮着建P1 P2 P3 P4 分区)create table sales2(sales_id int,sales_date date)partition by range(sales_date)--每十二个月划分一次interval(numtoyminterval(12,'month'))( --从2018 年1月1号开始partition p1 values less than(to_date('2018-01-01','yyyy-mm-dd')));--查看此时分区情况select * from user_tab_partitions where table_name='SALES2';insert into sales2( sales_id , sales_date) values(1,to_date('2021-04-01','yyyy-mm-dd'));insert into sales2( sales_id , sales_date) values(2,to_date('2020-04-01','yyyy-mm-dd'));insert into sales2( sales_id , sales_date) values(3,to_date('2019-04-01','yyyy-mm-dd'));insert into sales2( sales_id , sales_date) values(4,to_date('2018-04-01','yyyy-mm-dd'));--使用间隔分区查询数据select * from sales2 partition(SYS_P23);
十二、视图
--scott 创建视图create view view_emp_dept as select e.*,d.dname from emp e, dept dwhere e.deptno=d.deptno;--删除视图drop view view_emp_dept;--sys授权grant create view to scott;--使用视图select * from view_emp_dept;
十三、综合练习
--1.找出奖金高于薪金的50%的员工姓名。select e.ename from emp e where e.sal*0.5<e.comm;--2.查询没有奖金的员工姓名。select e.ename from emp e where e.comm is null or e.comm=0;--3.查询第二个字母为A的员工姓名select e.ename from emp e where e.ename like '_A%';--4.查询部门平均工资大于2000 的部门名称和平均工资。--第一种select d.dname,avg(e.sal) from emp e, dept d where e.deptno=d.deptno group by d.dname having avg(e.sal)>2000;--第二种select * from dept d join(select e.deptno de,avg(e.sal)from emp e group by e.deptno having avg(e.sal)>2000) d1on d.deptno=d1.de;select * from emp;
