视图view
描述视图
创建和修改删除视图
从视图中查询数据
通过视图插入,修改和删除数据
使用 "Top-N" 分析
基表:视图建立在已有表的基础上, 视图赖以建立的这些表称为基表
向视图提供数据的语句为 select 语句, 可以将视图理解为存储起来的select语句
视图向用户提供基表数据的另一种表现形式
好处:
控制数据访问
视图可以简化查询
避免重复访问相同的数据
--创建视图:
create view empview as select employee_id ,last_name,salary from employees where department_id = 80;
--创建视图可以使用多个表
create view empview2 as select employee_id,last_name,salary,department_name from employees e ,departments d where e.department_id = d.department_id;
--修改视图:
update empview set salary = 30000 where employee_id = 179;
--只读视图 read only
create view empview3 as select employee_id,last_name,salary,department_name from employees e ,departments d where e.department_id = d.department_id with read only;
复杂视图(与没有用到分组函数) or replace 重新创建一张视图把原先的覆盖了
create or replace view empview3 as select department_name dept_name,avg(salary) avg_sal from employees e,departments d where e.department_id = d.department_id group by department_name;
删除视图
drop view viewemp3;
Top - N分析: 嵌套两层 只能用< 和 <=
select rn,employee_id,last_name,salary from( select rownum rn,employee_id,last_name,salary from( select employee_id, last_name,salary from employees order by salary desc ) ) where rn>40 and rn<=50;
All that work will definitely pay off