Oracle資料庫檢視
阿新 • • 發佈:2019-01-27
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';
(從查詢結果可知,物化檢視在建立檢視的同時,也建立了一個同名的物理表,而物理表才是真正儲存資料的地方)
物化檢視使用場合:讀取頻繁,更新較少的情形下。
檢視功能總結:
增強安全性、組裝資料、封裝複雜查詢、提供建模模型、提高響應速度。