Orcl视图简单用法

来源:互联网 发布:vue.js 2.0中文文档 编辑:程序博客网 时间:2024/05/16 09:16

一、视图的作用
1、提供另一种级别的表级安全
2、隐藏数据的复杂性
3、简化查询语句
4、通过重命名列,从另一个角度提供数据安全
5、分离了应用查询与基础表
6、格式:

  create [or replace] [force|noforce] view  [schema.]<view_nane>  [column1,column2,...] as <查询语句>  [with check option [constraint<约束名>]]  [with read only]

二视图创建实例我们这里用scott作为用户
1、用system用户给 scott 用户赋予创建视图的权限

grant create view to scott;

2、创建视图

create view v_emp  as        select e.*,dname,loc from emp e,dept d where e.deptno=d.deptno;

3、通过视图查询用户信息

select * from v_emp;select * from v_emp where deptno=20;

4、根据部门来创建视图,不同的部门访问不同的视图

create view v_emp_20 as         select e.*,dname,loc from emp e,dept d where e.deptno=d.deptno and e.deptno=20;select * from v_emp_20;

5、with check option 用法

create or replace view v_emp1 as          select e.*,dname,loc from emp e,dept d where e.deptno=d.deptno and sal>2000 with check option;select * from v_emp1;

6、此时更新会失败,因为不满足视图中的条件

update v_emp1 set sal=1000 where empno=7782;

7、此时因为abc表不存在,所以视图创建失败,如果你认为这个表以后会有,我此时一定要创建这个视图,那么可以使用关键字force

create or replace force view v_abc as select * from abc;
1 0