创建视图

就是给查询语句取一个名称(别名) 类型: 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; 从回收站恢复表。