conn scott/tiger@orcl; create view v_scott_1 as select empNo,ename,job from soctt.emp wehre scott.emp.deptno=30;
--示例2 create view v_scott_2 (员工,姓名,职位) as select empno,ename,job from scott.emp where scott.emp.deptno=20;
--示例3 (创建视图后只能查询 不能 insert update delete) create view v_scott_3 as select empno, ename,job from emp where deptno =20 with read only;
--示例4 (视图创建后 能 insert update delete 但是 条件是 sal>2000) create view v_scott_4 as select empno,ename,job,sal from emp where sal>2000 with check option; insert itno v_soctt_4 values(7960,'jack','Clerk',1200); -- 报错 如果把1200 改为 sal>2000 就可以了
--2.强制创建视图 /*(正常情况下 如果基本表不存在 视图会创建失败。如果视图的语句没有错误,则使用Force 就可以创建视图 这种视图被称为:"带有编译错误的视图" 此时是处于失效状态 不能执行, 如果 之后基本表被创建了 该视图就可以正常使用) */ --示例1 create force view v_force_test as select c1,c2 from tb_force_test; select * from v_force_test; -- 会报错 (表或试图不存在) create table tb_force_test ( id number(20) primary key, c1 varchar2(20) not null, c2 varchar2(20) not null ); insert into tb_force_test values(1,'aaa','aaa'); select * from v_force_test; --能成功执行!
--3.可更新的连接视图 /*创建连接视图的select子句要满足一下条件 1. 不要集中运算符 union ,union all ,intersect ,minus 2.不包含 distinct 关键字 3.不包含 group by,order by,connect by start with 4.不包含子查询 5.不包含分组函数 6.需要定义的列不是列 表达式定义的 7.表中所有 not null 均属于该视图 8.只能对键值保存表 进行更改 (一般有父子关系组成的连接视图中,子表就是键值保存) */ --示例 1 (deptno 是emp 的父表 ,所以 emp 是键值保存表,只能更新这个表 而且 这个表中的主键 唯一键 都在这个视图中) create or replace view v_dept_emp_1 as select b.empno,b.ename,a.deptno,a.loc from dept a ,emp b,where a.deptno=b.deptno and a.deptno in (10,30); update v_dept_emp_1 set ename='JACK' where empno=7521; --可以成功执行 update v_dept_emp_1 set loc='BEIJIN' where deptno=30; --错误 不能修改非键值保存表 对应的列
--4.查询视图的可更新列 (使用User_UpdateTable_Columns 数据库字典:查询当前用户方案中所有的表和视图可修改的列) select a.column_name,a.updatable,a.insertable,a.insertable,a.deletable from user_updatable_columns a where a.table_name=upper('v_dept_emp_1');
--5.查询视图的定义信息 (使用User_views 数据库字典 :查询当前方案中视图的定义信息。) column view_name format a16 column text format a63 select c.view_name,c.text from user_views c where c.view_name=upper('v_dept_emp_1');
--6.删除视图 (要权限 drop any view) conn sys/admin@orcl; grant drop any view to scott;