一:视图创建
create [or replace] [force|noforce] view 视图名
[(别名)[,别名1]....]
as
子查询
[with check option [constraint 约束名]]
[with read only]
参数说明
or replace 如果该表已经存在就覆盖掉
force不管所引用的表是否存在,Oracle都会创建这个视图
noforce只有当所有的表都存在的时候,Oracle才会创建这个视图
with check option 所插入或修改的数据行必须满足视图是我所有定义的约束条件
with read only 保证在该视图上不能进行任何DML操作
create view avgdept
as
select d.dname "部门名称",avg(e.sal) "平韵工资",avg(nvl(comm,0)) "平均佣金",count(*) "员工数"
from emp e,dept d
where d.deptno=e.deptno
group by d.dname
SQL> select * from avgdept;
部门名称 平韵工资 平均佣金 员工数
-------------- ---------- ---------- ----------
ACCOUNTING 2916.66667 0 3
RESEARCH 2175 0 5
SALES 1514.28571 314.285714 7
【实例2】
SQL> create or replace view acct
2 ("姓名","工资","职位","雇佣日期")
3 as
4 select ename,sal ,job,hiredate
5 from emp
6 where deptno=10;
二:Oracle如何管理视图
SQL> select view_name,text_length,text
2 from user_views;
三:如何使用视图的with check option子句
create view sales30
as
select * from emp
where deptno=30
with check option constrait sales30_ck;
测试:
update sales30
set deptno=10
where job='manager';
这时候的更新时失败的因为他违反了创建视图的where deptno=30这句话with check option constrait sales30_ck;这句话就是要来控制where的条件限制
6万+

被折叠的 条评论
为什么被折叠?



