Oracle_071_lesson_p14
创建视图就是给查询语句取一个名称(别名)
类型:
1、简单视图 simpleview
2、复杂视图 complexview
CREATE VIEW view
[(alias[, alias]...)]
AS subquery
]
];
简单视图 simpleview
示例:
createviewempv80
as
selectsalary
from employees
wheredepartment=80;
descempv80;
select*from empv80;
CREATE VIEW salvu50
AS
SELECTemployee_id> salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;
复杂视图 complexview
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECTemployee_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';
setlong 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*12year_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
检查约束
withcheckoptionconstraintempv20_ck;
CREATE OR REPLACE VIEW empvu20
AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck ;
withread 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;
droptable emp purge; 不入回收站,直接删除。
flashbacktableempto beforedrop; 从回收站恢复表。
页:
[1]