Home > Database > Mysql Tutorial > ORACLE 视图(VIEW)

ORACLE 视图(VIEW)

WBOY
Release: 2016-06-07 15:24:49
Original
1442 people have browsed it

说ORACLE视图之前,有这样的需求我们先来想一下。 有一张老师信息表,表中字段有:老师ID、姓名、婚姻状况、身份证号、代课科目;其中婚姻状况、身份证号教务处人可以查看,但对学生是禁止查看、保密的。此两种身份都用的同一张表,查看不同的结果怎么实现?

说ORACLE视图之前,有这样的需求我们先来想一下。

有一张老师信息表,表中字段有:老师ID、姓名、婚姻状况、身份证号、代课科目;其中婚姻状况、身份证号教务处人可以查看,但对学生是禁止查看、保密的。此两种身份都用的同一张表,查看不同的结果怎么实现? .........这就用到了视图,限制需要保密的字段信息被学生看到。

视图时基于真实表的一个展现。它只是一个逻辑概念,视图中的数据来源于基表(真实表),几乎不占用物理存储空间;视图和普通表一样,可以进行增删改操做;视图保证了数据库数据的安全性。当然还有方便快捷性(如较复杂的SQL条件查询可以定义到一个视图中,直接查询该视图) 。先来了解如何定义一个视图:

1、视图完整定义语法:

CREATE[OR REPLACE][FORCE][NOFORCE]VIEW view_name

[(column_name)[,….n]]
 
AS
 
Select_statement
 
[WITH CHECK OPTION[CONSTRAINT constraint_name]]                
 
[WITH READ ONLY]  

OR REPLACE  --如视图存在替换存在试图

FORCE][NOFORCE  --基表不存在FORCE强行创建; NOFORCE不创建(默认)

WITH CHECK OPTION[CONSTRAINT constraint_name]   --对视图DML操纵时,是否验证定义视图SQL的WHERE条件。

WITH READ ONLY  --定义只读视图

2、创建视图、以基表classes为例

SQL> SELECT * FROM classes;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902
                                      3 0903

为classes表创建默认视图classes_view语句

SQL> CREATE OR REPLACE NOFORCE VIEW  classes_view
  2  AS
  3  SELECT * FROM classes
  4  ;
View created

创建视图成功;上面说到视图和普通表一样,可以进行增删改操作,以新增数据到视图为例说明(修改、删除同新增一样这里不一一列举)

SQL> insert into classes_view values(4,1111);
1 row inserted

SQL> commit;
Commit complete

SQL> SELECT * FROM classes_view;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902
                                      3 0903
                                      4 1111

同样基表中也存在该条新增数据。

SQL> SELECT * FROM classes;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902
                                      3 0903
                                      4 1111

为classes表创建带查询条件视图classes_view 

SQL> CREATE OR REPLACE NOFORCE VIEW  classes_view
  2  AS
  3  SELECT * FROM classes WHERE cid
  4  ;
View created

SQL> SELECT * FROM CLASSES_VIEW;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902

--因为创建视图时带WHERE条件cid

SQL> SELECT * FROM CLASSES;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902
                                      3 0903
                                      4 1111

在CLASSES_VIEW视图新增一条cid为5的信息

SQL> INSERT INTO CLASSES_VIEW VALUES(5,'2222');
1 row inserted

SQL> COMMIT;
Commit complete

--视图上添加数据成功。查询视图显示结果:

SQL> SELECT * FROM CLASSES_VIEW;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902

???怎么没包含我们刚新添加的数据呢?新增数据上哪去了?我们上面说到,操作视图数据实际上操作的是基表,也就是数据保存到了基表CLASSES表中。那么为什么我们定义的视图中查不到了,回看我们视图定义语法:

SQL> CREATE OR REPLACE NOFORCE VIEW  classes_view
  2  AS
  3  SELECT * FROM classes WHERE cid
  4  ;

是因为有WHERE条件限制的,带条件视图不显示没有满足条件的数据,所以也就看不到。那有人就要问了,既然视图只显示满足WHERE条件的信息,那么如何控制不满足视图WHERE条件的信息从视图中录入(insert,update)呢? 使用视图关键字:WITH CHECK OPTION ,下面讲解WITH CHECK OPTION如何控制非满足条件如何禁止从视图中录入

--为classes表创建带查询条件WITH CHECK OPTION视图classes_view

SQL> CREATE OR REPLACE NOFORCE VIEW  classes_view
  2  AS
  3  SELECT * FROM classes WHERE cid
  4  WITH CHECK OPTION
  5  ;
View created

SQL> select * from classes_view;
                                    CID CNAME
--------------------------------------- ----------
                                      1 0901
                                      2 0902

创建视图成功,视图中只包含约束条件cid

SQL> INSERT INTO CLASSES_VIEW VALUES(5,2222);
INSERT INTO CLASSES_VIEW VALUES(5,2222)
ORA-01402: 视图 WITH CHECK OPTIDN where 子句违规

插入满足where条件数据就能插入成功,如下插入

SQL> INSERT INTO CLASSES_VIEW VALUES(0,'0001');
1 row inserted

SQL> commit;
Commit complete

SQL> select * from classes_view;
                                    CID CNAME
--------------------------------------- ----------
                                      0 0001
                                      1 0901
                                      2 0902

修改视图:ORACLE没有提供修改视图的操纵语句,如想修改视图使用replace关键字

CREATE OR REPLACE VIEW  classes_view
AS
SELECT * FROM classes

View created

删除视图

SQL> DROP VIEW classes_view;
View dropped


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template