一:概念
视图是基于一张表或多张表或另外一个视图的逻辑表。视图不同于表,视图本身不包含任何数据。表是实际独立存在的实体,是用于存储数据的基本结构。而视图只是一种定义,对应一个查询语句。视图的数据都来自于某些表,这些表被称为基表。数据库中只在数据字典中存储对视图的定义。
二:优点
1、为用户集中数据,简化用户的数据查询和处理。
2、屏蔽数据库的复杂性,用户不必了解数据库的复杂性。
3、简化用户权限的管理,只授予用户使用视图的权限。
4、可以提高数据访问的安全性,通过视图往往只可以访问数据库中表的特定部分,限制了用户访问表的全部行和列。
5、便于数据共享,多个用户不必都定义所需的数据。
三:视图类别
1、简单视图
指基于单个表并且不包含函数或表达式的视图,在该视图上可以执行DML语句(即可执行增、删、改操作)。
2、复杂视图
指基于单个或者多个表或者包含函数、表达式或者分组数据的视图,在该视图上执行DML语句时必须要符合特定条件。注意:在定义复杂视图时必须为函数或表达式定义别名
3、连接视图
指基于多个表建立的视图,一般来说不会在该视图上执行INSERT、UPDATE、DELETE操作。
4、只读视图
指只允许进行SELECT操作的视图,在该视图时指定WITH READ ONLY选项。该视图上不能执行INSERT、UPDATE、DELETE操作。
5、check约束视图
WITH CHECK OPTION用于在视图上定义CHECK约束,即在该视图上执行INSERT或UPDATE操作时,数据必须符合查询结果.
2、参数说明
1
2
3
|
CREATE
OR
Repalce:用于创建和修改视图
WITH
CHECK
OPTION
:用于创建限制数据访问的视图
WITH
READ
ONLY
:用于创建只读视图
|
3、DML操作遵循的原则
a)简单视图可以执行DML操作。
b)在视图出现下列情况时不可以通过视图修改基表数据或插入数据:
i、集合运算符(union,intersect,minus)
ii、DISTINCT关键字
iii、GROUP BY,ORDER BY,CONNECT BY或START WITH子句
v、子查询
vi、分组函数
vii、需要更新的列不是由“列表达式”定义的
vx、基表中所有NOT NULL列均属于该视图
五:如何查询视图和表的更新权限
1
2
|
select
table_name,column_name,updatable,insertable,deletable
from
user_updatable_columns;
|
说明
updatable 表示当前字段是否可以执行修改操作
insertable 表示当前字段是否可以执行添加操作
deletable 表示当前字段是否可以执行删除操作
六:示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
--对简单视图的操作
drop
table
emp1;
create
table
emp1
as
select
*
from
emp;
--简单视图
create
or
replace
view
v_emp1
as
select
*
from
emp1 ;
--查询
select
*
from
v_emp1;
--更新
update
v_emp1
set
v_emp1.ENAME=
'andy'
where
v_emp1.JOB=
'CLERK'
;
--增加
insert
into
v_emp1
values
(7777,
'chy'
,
'MANAGER'
, 8888, sysdate, 10000,1111.11,20);
--删除
delete
from
v_emp1
where
v_emp1.EMPNO=7777;
--复杂视图、仅两基表相连、不包含各种分组函数、group by、distinct命令等。
create
or
replace
view
v_complex
as
select
emp1.ename, emp1.job, dept.dname
from
emp1, dept
where
emp1.deptno=dept.deptno
with
check
option
;
--查询
select
*
from
v_complex;
--修改
update
v_complex
set
v_complex.ename=
'andy'
where
v_complex.job=
'MANAGER'
;
--增加
--报错:ORA-01776:无法通过连接视图修改多个基表
insert
into
v_complex (v_complex.ename, v_complex.job, v_complex.dname)
values
(
'chy'
,
'MANAGER'
,
'SALES'
);
--删除
delete
from
v_complex
where
v_complex.ename=
'chy'
;
--复杂视图、包含不能进行DML的元素、一般仅用与查询、可以加上 with read only;
create
or
replace
view
v_complex_readonly
as
--对使用聚合函数的列必须使用别名!
select
max
(emp1.sal) max_sal
from
emp1, dept
where
emp1.deptno=dept.deptno
group
by
dept.deptno
with
read
only
;
--查询
select
*
from
v_complex_readonly;
--删除视图
create
or
replace
view
v_for_delete
as
select
*
from
emp
with
read
only
;
drop
view
v_for_delete;
|
实例:
select * from student --创建视图 create view V_student as select sname, sclass from student --修改视图 create or replace view v_student as select son,sname, sclass from student select * from v_student; --对视图进行操作就和对普通表进行操作是一样的了。 update v_student set sname='花荣' where son=112; --变成只读视图 create view v_student2 as select sname from student with read only create or replace view v_student2 as select son,sname from student with read only update v_student2 set sname='花荣' where son=112