Oracle数据库视图

Oracle中有四种视图:
关系视图
内嵌视图
对象视图
物化视图

需要的表和数据信息:

create table employees(employee_id number primary key , first_name varchar2(4), last_name varchar2(4), province varchar2(10),city varchar2(10),salary number);
insert into employees values(1,'军','张','广州','深圳',3000);
insert into employees values(2,'新','刘','广东','深圳',4000);
insert into employees values(3,'凡','王','广西','桂林',3800);
insert into employees values(4,'峰','周','江苏','苏州',4000);
insert into employees values(5,'佳','徐','浙江','杭州',2900);
create table employees_sales(id number,sale_month varchar2(4),total_price number,sale_by number);
insert into employees_sales values(1,'3月',1200,1);
insert into employees_sales values(1,'3月',1500,2);
insert into employees_sales values(1,'3月',3000,3);
insert into employees_sales values(1,'3月',14800,4);
insert into employees_sales values(1,'3月',1760,5);
create table employee_sale_details (sale_details_id number,goods_name varchar2(10),price number,quantity number,sale_month varchar2(10),sale_by number);
insert into employee_sale_details values(1,'西装',300,2,'3月',1);
insert into employee_sale_details values(2,'毛衣',20,2,'3月',1);
insert into employee_sale_details values(3,'西装',100,2,'3月',1);
insert into employee_sale_details values(4,'运动鞋',80,2,'3月',2);
insert into employee_sale_details values(5,'皮鞋',300,2,'3月',2);
insert into employee_sale_details values(6,'T恤',100,2,'3月',2);
insert into employee_sale_details values(7,'羊毛衫',70,2,'3月',3);
insert into employee_sale_details values(8,'西装',50,2,'3月',3);
insert into employee_sale_details values(9,'西装',60,2,'3月',3);
insert into employee_sale_details values(10,'羊毛衫',100,2,'3月',4);
insert into employee_sale_details values(11,'西装',40,2,'3月',4);
insert into employee_sale_details values(12,'皮鞋',80,2,'3月',4);
insert into employee_sale_details values(13,'皮鞋',60,2,'3月',5);
insert into employee_sale_details values(14,'毛衣',300,2,'3月',5);
insert into employee_sale_details values(15,'西装',300,2,'3月',5);

1、关系视图(作为查询定义,一旦创建,即可存在于数据库中,并可被多次使用。)

创建视图:
create or replace view vw_employees as select employee_id,last_name || first_name as employee_name,province ||'-'||city as location from employees;

查看视图定义:
select text from user_views where view_name = 'VW_EMPLOYEES';

删除视图:
drop view vw_employees;

联接视图:
create or replace view vw_employees_salary as select 
 e.employee_id,e.last_name || e.first_name as employee_name,e.province ||'-'||e.city as location,
e.salary+s.total_price*0.1 as salary
from employees e,employees_sales s
where e.employee_id = s.sale_by and s.sale_month = '3月';


编译视图:
查询视图状态:
select object_name,status from user_objects where object_name = 'VW_EMPLOYEES_SALARY' and object_type = 'VIEW';  --状态字段为VALID,为有效
当基表结构变化后,视图的状态变为INVALID,这时需要重新编译视图(或者重新查询视图也相当于重新编译视图):
alter view vw_employees_salary compile;  --重新编译后视图状态被重置为VALID

关系视图好处:
(1)保障数据安全性;
(2)数据整合;
(3)数据透明性;

2、内嵌视图:

被嵌套在查询语句中使用的视图,功能类似于子查询;
(也即:代替了表的位置的查询就是内嵌视图,内嵌视图的定义必须用小括号括起来)
--使用别名的内嵌视图
select e.employee_id,e.last_name||e.first_name employee_name,e.salary+s.total_price*0.1 salary
from employees e,
     (select sale_by,sum(price*quantity) total_price from employee_sale_details group by sale_by) s
where e.employee_id = s.sale_by;
--不使用别名的内嵌 视图
select * from (
          select employee_id,last_name||first_name employee_name,salary from employees order by salary desc
      ) where rownum <=3;

3、对象视图:

4、物化视图(实体化视图):
物理化视图的简称,该视图存储实际数据
创建物化视图:

create materialized view vw_monthly_sales
build immediate
refresh on commit
--enable query rewrite
as
 select sale_month,sum(price*quantity)
 from employee_sale_details
 group by sale_month;

查询物化视图:
select mview_name,query from user_mviews where mview_name = 'vw_monthly_sales';

在user_objects中查询相关信息:
select object_name,object_type,status from user_objects where object_name = 'vw_monthly_sales';
(从查询结果可知,物化视图在创建视图的同时,也创建了一个同名的物理表,而物理表才是真正存储数据的地方)

物化视图使用场合:读取频繁,更新较少的情形下。

视图功能总结:
     增强安全性、组装数据、封装复杂查询、提供建模模型、提高响应速度。


你可能感兴趣的:(MySql/Oracle)