基本概念: 视图: 从表中抽出的逻辑上相关的数据集合。 视图是一种虚表。 视图建立在已有表的基础上, 视图赖以建立的这些表称为基表。 向视图提供数据内容的语句为 SELECT 语句, 能够将视图理解为存储起来的 SELECT 语句. 视图向用户提供基表数据的还有一种表现形式 为什么使用视图? 控制数据訪问 简化查询 避免反复訪问同样的数据 经常使用脚本: --创建视图 create view v_emp as select * from employees t where t.department_id = '60'; select * from v_emp; update v_emp01 t set t.SALARY=11100; select * from employees where employees.first_name='Alexander'; --多表连接创建视图 create view v_emp00 as select e.employee_id id , e.last_name name ,e.salary , d.department_name from employees e ,departments d where e.department_id = d.department_id; --改动视图 create or replace view v_emp00 as select e.employee_id id , e.last_name name , d.department_name from employees e ,departments d where e.department_id = d.department_id; --使视图仅仅有读的操作 create view v_emp01 as select e.employee_id id , e.last_name name ,e.salary , d.department_name from employees e ,departments d where e.department_id = d.department_id with read only; --复杂视图 使用了组函数 是不能进行增删改的 create view v_emp02 as select department_name ,avg(salary) avg_sal from employees e ,departments d where e.employee_id = d.department_id group by department_name; --top N 工资最高的前十个人 select rownum employee_id , first_name ,salary from (select employee_id ,first_name ,salary from employees order by salary desc) where rownum <= 10; --对 ROWNUM 伪列 仅仅能使用 < 或 <=, 而用 =, >, >= 都将不能返回不论什么数据,下面sql查不到数据 select rownum employee_id , first_name ,salary from (select employee_id ,first_name ,salary from employees order by salary desc) where rownum <= 50 and rownum >40 ; --通过下面方式解决 select rn employee_id , first_name ,salary from( select rownum rn ,employee_id , first_name ,salary from (select employee_id ,first_name ,salary from employees order by salary desc) )where rn <= 50 and rn >40 ;