01-07 Oracle数据开发技术之视图、索引和同义词(BOBO)(完成).docxVIP

  • 0
  • 0
  • 约3.66万字
  • 约 31页
  • 2023-06-13 发布于北京
  • 举报

01-07 Oracle数据开发技术之视图、索引和同义词(BOBO)(完成).docx

第十三章:视图 视图本身是一个不包含任何真实数据,只存放视图的定义的虚拟表,数据仍存放在原来的实体表中。 创建视图 create [or replace] [force | noforce] view 视图名称 as 子查询 [with check option] [with read only] force:表示要创建视图的表存在也可以创建视图。 noforce:表示要创建视图的表必须存在,否则无法创建。 or replace:表示视图的替换,如果创建的视图不存在,则创建新的而试图,如果视图已经存在,则将其替换。 视图的作用 可以加强数据的保密性 可以减少查询的复杂性 视图的DML操作 简单视图的DML操作(一个表) 复杂视图的DML操作(多张表) [with check option]选项 [with read only]选项 实例13.1 授权scott用户有视图的权限 //登录sys,授权scott有建立视图的权限 Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as sys@BEDU AS SYSDBA SQL grant create any view to scott; Grant succeeded SQL 实例13.2 视图实现过程 步骤1:查询部门表 select d.deptno,d.dname,d.loc from dept d 步骤2:查询员工表部分统计信息 select e.deptno,count(*) num,round(avg(e.sal)) avg_sal from emp e group by e.deptno 步骤3:查询员工表信息 select * from emp; 步骤4:删除之前的测试数据 delete from emp where empno in(1111,2222,8888,9999); 步骤5:再次查询员工表信息 select * from emp; 步骤6:笛卡尔查询 select d.deptno, d.dname, d.loc,f.num,f.avg_sal from dept d, (select e.deptno, count(*) num, round(avg(e.sal)) avg_sal from emp e group by e.deptno) f where d.deptno = f.deptno 步骤7:将步骤6的笛卡尔查询写入视图 create or replace view v_dept_statics as select d.deptno, d.dname, d.loc,f.num,f.avg_sal from dept d, (select e.deptno, count(*) num, round(avg(e.sal)) avg_sal from emp e group by e.deptno) f where d.deptno = f.deptno(+) 步骤8:查询视图 select * from v_dept_statics; 步骤9:实体表不存在的时候,报错 create or replace view v_emp as select * from emps; 步骤10:先建立视图,不管是否有实体表 create or replace force view v_emp as select * from emps; 步骤11:查询视图v_emp select * from v_emp; 步骤12:创建emps表 create table emps as select * from emp; 步骤13:右键重新编译v_emp视图 步骤14:再次查询视图v_emp select * from v_emp; 步骤15:限制信息显示,提高数据安全性 create or replace force view v_emp as select empno,ename,job,mgr,hiredate,deptno from emps where deptno=30; 步骤16:再次查询视图v_emp select * from v_emp; 实例13.3简单视图的DML操作 步骤1:查看视图 select * from v_emp; 步骤2:查看视图对应的实体表 select * from emps; 步骤3:简单视图增加记录 insert into v_emp(empno,ename,

文档评论(0)

1亿VIP精品文档

相关文档