CREATE [OR REPLACE] [FORCE]VIEWview_name [(alias[, alias]...)]
AS
select_statement[WITH CHECK OPTION]
[WITH READ ONLY];
SQL> create view test_view_force as select * from student;
create view test_view_force as select * from student
ORA-00942: 表或视图不存在
SQL> create force view test_view_force as select * from student;
Warning: View created with compilation errors
SQL> select * from user_views uv where uv.VIEW_NAME='TEST_VIEW_FORCE';
VIEW_NAME TEXT_LENGTH TEXT TYPE_TEXT_LENGTH TYPE_TEXT OID_TEXT_LENGTH OID_TEXT VIEW_TYPE_OWNER VIEW_TYPE SUPERVIEW_NAME EDITIONING_VIEW READ_ONLY
------------------------------ ----------- -------------------------------------------------------------------------------- ---------------- -------------------------------------------------------------------------------- --------------- -------------------------------------------------------------------------------- ------------------------------ ------------------------------ ------------------------------ --------------- ---------
TEST_VIEW_FORCE 22 select * from student N N
SQL> SELECT * FROM TEST_VIEW_FORCE;
SELECT * FROM TEST_VIEW_FORCE
ORA-04063: view "HR.TEST_VIEW_FORCE" 有错误
SQL> create table student(id number(20),name varchar2(20), enter_date date );
Table created
SQL> insert into student values(1,'张三',to_date('20140101','yyyymmdd'));
1 row inserted
SQL> insert into student values(1,'李四',to_date('20140102','yyyymmdd'));
1 row inserted
SQL> insert into student values(1,'王五',to_date('20140103','yyyymmdd'));
1 row inserted
SQL>
SQL> select * from test_view_force;
ID NAME ENTER_DATE
--------------------- -------------------- -----------
1 张三 01-1月-14
1 李四 02-1月-14
上面的测试时针对于force关键字的,通过force关键字可以预先为某个即将创建的表先创建视图。
|
SQL> create view test_view_with_check_option as select * from student where student.name='张三';
View created
SQL> select * from test_view_with_check_option;
ID NAME ENTER_DATE
--------------------- -------------------- -----------
2 张三 01-1月-14
SQL> update test_view_with_check_option tvwco set tvwco.ID=10 where tvwco.NAME='张三';
1 row updated
SQL>
select * from test_view_with_check_option;
ID NAME ENTER_DATE
--------------------- -------------------- -----------
10 张三 01-1月-14
SQL> select * from student;
ID NAME ENTER_DATE
--------------------- -------------------- -----------
10 张三 01-1月-14
4 李四 02-1月-14
4 王五 03-1月-14
SQL>
更新视图也更新表
上面的测试本来是验证假如没有加with check option就会出现 红色的查询会查不出来,会出现丢失行的问题
但是测试没有成功....反正记住这一点加了with check option就是为了视图不能更新
with reed only 不能更新
|
SQL> purge recyclebin;//清除回收站
Done
SQL> create table student (sno number(20),sname varchar2(20),sdept number(20));
Table created
SQL> create table dept (deptno number(20),deptname varchar2(20));
Table created
SQL> insert into student values(1,'张三',100);
1 row inserted
SQL> insert into student values(2,'李四',101);
1 row inserted
SQL> insert into student values(3,'李四',102);
1 row inserted
SQL> insert into dept values(100,'数计系');
1 row inserted
SQL> insert into dept values(101,'医学系');
1 row inserted
SQL> insert into dept values(102,'文学系');
1 row inserted
SQL> alter table student add constraints pk_student primary key(sno);
Table altered
SQL> alter table dept add constraints pk_dept primary key(deptno);
Table altered
SQL>
SQL> alter table student add constraints fk_foreign foreign key(sdept) references dept(deptno);
Table altered
SQL> create view view_stu_dept
2 as
3 select * from student s inner join dept d on s.sdept = d.deptno;
View created
SQL> select * from view_stu_dept;
SNO SNAME SDEPT DEPTNO DEPTNAME
--------------------- -------------------- --------------------- --------------------- --------------------
1 张三 100 100 数计系
2 李四 101 101 医学系
3 李四 102 102 文学系
SQL> select uc.OWNER,uc.CONSTRAINT_NAME,uc.CONSTRAINT_TYPE,uc.TABLE_NAME,uc.view_related from user_constraints uc where uc.TABLE_NAME='STUDENT';
OWNER CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME VIEW_RELATED
------------------------------------------------------------ ------------------------------ --------------- ------------------------------ --------------
HR PK_STUDENT P STUDENT
HR FK_FOREIGN R STUDENT
SQL> update view_stu_dept vsd set vsd.SNO=11 where vsd.SNO = 1;
1 row updated
SQL> select * from student;
SNO SNAME SDEPT
--------------------- -------------------- ---------------------
11 张三 100
2 李四 101
3 李四 102
SQL> update view_stu_dept vsd set vsd.SDEPT=103 where vsd.SNO = 2;
update view_stu_dept vsd set vsd.SDEPT=103 where vsd.SNO = 2
ORA-02291: 违反完整约束条件 (HR.FK_FOREIGN) - 未找到父项关键字
SQL> update view_stu_dept vsd set vsd.DEPTNO=103 where vsd.SNO = 2;
update view_stu_dept vsd set vsd.DEPTNO=103 where vsd.SNO = 2
ORA-01779: 无法修改与非键值保存表对应的列
SQL>
|
SQL> create view view_stu_fun
2 as
3 select sno, upper(student.sname) ,student.sdept from student s ;
create view view_stu_fun
as
select sno, upper(student.sname) ,student.sdept from
student s
ORA-00904: "STUDENT"."SDEPT": 标识符无效
上面错误说明了假如给表起了别名那么就必须使用指定别名来引用列而不能使用
表名.列名
SQL>
SQL> create view view_stu_fun
2 as
3 select sno, upper(student.sname) ,student.sdept from student
4
SQL> create view view_stu_fun
2 as
3 select sno ,upper(sname),sdept from student;
create view view_stu_fun
as
select sno ,upper(sname),sdept from student
ORA-00998: 必须使用列别名命名此表达式
使用函数创建视图必须为视图起别名
SQL> create view view_stu_fun
2 as
3 select sno ,upper(sname) as upper_sname,sdept from student;
View created
SQL> select * from student;
SNO SNAME SDEPT
--------------------- -------------------- ---------------------
1 zhangsan 100
2 李四 101
3 李四 102
SQL> select * from view_stu_fun;
SNO UPPER_SNAME SDEPT
--------------------- -------------------- ---------------------
1 ZHANGSAN 100
2 李四 101
3 李四 102
SQL> update view_stu_fun set UPPER_SNAME = '张三' where UPPER_SNAME='zhangsan';
update view_stu_fun set
UPPER_SNAME= '张三' where UPPER_SNAME='zhangsan'
ORA-01733: 此处不允许虚拟列
更新视图时不能使用虚拟列
SQL> update view_stu_fun set SNO = 4 where UPPER_SNAME='ZHANGSAN';
1 row updated
SQL>
|