Oracle_071_lesson_p14

創建視圖

就是給查詢語句取一個名稱(別名)
類型:
1、簡單視圖 simple view
2、複雜視圖 complex view

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];

簡單視圖 simple view
示例:
create view empv80
as
select salary
from employees
where department=80;

desc empv80;
select * from empv80;

CREATE VIEW salvu50
AS
SELECT employee_id ID_NUMBER, last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;

複雜視圖 complex view

CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' '
|| last_name, salary, department_id
FROM employees
WHERE department_id = 80;

CREATE OR REPLACE VIEW dept_sum_vu
(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
MAX(e.salary),AVG(e.salary)
FROM employees e JOIN departments d
USING (department_id)
GROUP BY d.department_name;
此select 語句複雜些,可包含函數等

DESCRIBE user_views;

SELECT view_name FROM user_views;

SELECT text FROM user_views
WHERE view_name = 'EMP_DETAILS_VIEW';

set long 9999;

select 子句有表達式或者數字時,要加別名。
不能刪除視圖,比如有:
1、group by
2、distinct
3、rownum
4、分組函數
You can usually perform DML operations on simple views.
You cannot remove a row if the view contains the following:
1、Group functions
2、A GROUP BY clause
3、The DISTINCT keyword
4、The pseudocolumn ROWNUM keyword

不能修改視圖,比如有:
1、group by
2、distinct
3、rownum
4、分組函數
5、表達式(如salary*12 year_salary)
You cannot modify data in a view if it contains:
1、Group functions
2、A GROUP BY clause
3、The DISTINCT keyword
4、The pseudocolumn ROWNUM keyword
5、Expressions

You cannot add data through a view if the view includes:
1、Group functions
2、A GROUP BY clause
3、The DISTINCT keyword
4、The pseudocolumn ROWNUM keyword
5、Columns defined by expressions
6、NOT NULL columns without default value in the base tables that are not selected by the view

檢查約束
with check option constraint empv20_ck;
CREATE OR REPLACE VIEW empvu20
AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck ;

with read only ;
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 view;
DROP VIEW empvu80;

drop table emp purge; 不入回收站,直接刪除。
flashback table emp to before drop; 從回收站恢復表。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章