oracle视图

来源:互联网 发布:英语电影观后感的软件 编辑:程序博客网 时间:2024/04/29 13:08

oracle视图

1、  视图是一种虚表

2、  视图建立在已有表的基础上,视图赖以建立的这些表称为基表

3、  向视图提供数据内容的语句为select语句,可以将试图理解为存储起来的select语句

4、  视图向用户提供基表数据的另一种表现形式

为什么使用视图

1、  控制数据访问

2、  简化查询

3、  避免重复访问相同的数据

创建视图

1、  在create  view语句中嵌入子查询

Create [or  replace][force|noforce] view view [(alias[,alias]…)]

As subquery [with check  option [constraint  constraint]]

                      [with read   only [constraint  constraint]]

2、  子查询可以是复杂的select语句

例子:

Create  view  empvu80

As  select  employee_id,last_name,salary  from employees  wheredepartment_iid=80;

3、  创建视图时在子查询中给列定义别名

Create  view salvu50as select employee_id id_number,last_name name,salary*12  ann_salary  from employees where department_id=50;

4、  在选择视图中的列时应使用别名

查询视图

         Select  * from  salvu50;

修改视图

1、  使用create  or replace  view字句修改视图

Create  or  replace view empvu80( id_number,name,sal,department_id)

As  selectemployee_id,first_name|| ‘’||last_name,salary,department_id

From   employeeswhere department_id=80;

2、  create  view 字句中各列的别名应和子查询中各列相对应

视图中使用DML的规定

1、  可以在简单视图中执行DML操作

2、  当视图定义中包含以下元素之一不能使用delete

a)        组函数

b)        Group  by字句

c)        Distinct关键字

d)        Rownum伪列

3、  当视图定义中包含以下元素之一不能使用update

a)        组函数

b)        Group  by字句

c)        Distinct关键字

d)        Rownum伪列

e)        列的定义为表达式

4、  当视图定义中包含以下元素之一不能使用insert

a)        组函数

b)        Group  by字句

c)        Distinct关键字

d)        Rownum伪列

e)        列的定义为表达式

f)         表中非空的列在视图的定义中未包含

屏蔽DML操作

1、  可以使用with  read only选项屏蔽对视图的DML操作

2、  任何DML操作都会返回一个Oracle  Server错误

例子:

Create  or  replace view empvu10(employee_number,employee_name,job_title)

As  select  employee_id,last_name,job_id  from employees  where  department_id=10  with read  only;

删除视图

         删除视图只是删除视图的定义,并不会删除基表的数据

         Drop  view  empvu80;