视图

来源:互联网 发布:youtube显示无网络连接 编辑:程序博客网 时间:2024/06/05 03:06

视图, 默认指的是关系视图, 又叫虚表。

  • 不占用数据空间
  • 可以简化语句
  • 可以隐藏细节
  • 可以提升安全

除了关系视图,广义的视图包括:

  1. 关系视图(狭义的视图,虚表)
  2. 内嵌视图(子查询中的临时结果)
  3. 对象视图(面向对象)
  4. 物化视图(以空间换时间)
-- 查询跟视图相关的权限select * from system_privilege_map where name like '%VIEW%';-- 确保用户有足够权限grant create view to vip;grant all on scott.emp to vip;grant all on scott.dept to vip;-- 创建视图create view vemp as select * from scott.emp;  -- 最简形式,为单个表创建视图create view v_emp_vip as select * from scott.emp where sal >= 2000;  -- 可以为表的部分数据创建视图create view vvevip as select * from vevip;  -- 可以为视图查询创建视图create or replace view vevip as     -- 可以为多表联合查询的结果集创建视图。  select e.*, d.dname, d.loc    from emp e, dept d   where e.deptno = d.deptno(+)     and e.sal >= 2000;-- 使用select * from e;select * from vemp;select * from v_emp_vip;select * from vevip;select * from vvevip;-- 删除drop view vemp;-- force 强迫!!!不管 select 语句是否有错,都要创建。-- or replace, 如果视图已存在,覆盖-- with read only, 创建只读视图create or replace force view vaaa as select * from wohaoshuai with read only;-- with check option, 防止更新后的数据, 超出视图的范围insert into emp (empno, ename, sal) values (9988, '张思', 3333);create or replace view vbbb as select * from emp where sal > 3332;select * from vbbb; -- 两条数据,King 和 张思update vbbb set sal = 2000 where empno = 9988;select * from vbbb; -- 只剩一条数据。-- 按照道理,update 只是更新数据,不应该导致结果集变少。所以,需要通过 with check option 来限制不合理的修改create or replace view vcc as select * from emp where sal > 1500  with check option;update vbbb set sal = 1000 where empno = 9988;  -- 会报错
原创粉丝点击