Oracle创建视图
在本练习中,将在HR模式中练习如何创建视图,查询视图的定义,并对视图进行更新。
(1)创建一个视图EMPLOYEES_IT,该视图是基于HR模式中的EMPLOYEES表,并且该视图只包括那些部门为IT的员工信息。
在创建视图时使用WITH CHECK OPTION,防止更新视图时,输入非IT部门的员工信息。
create or replace view employees_it as
select *
from employees
where department_id =(
select department_id from departments
where departments.department_name='IT')
with check option;
(2)创建一个联接视图EMP_DEPT,它包含EMPLOYEES表中的列和DEPARTMENTS 表中的DNAME列。
create or replace view emp_dept as
select t1.employee_id,t1.first_name,st_name,t1.email,
t1.phone_number,t1.hire_date,t1.job_id,t1.salary,t2.department_name
from employees t1,departments t2
where t1.department_id=t2.department_id
with check option;
(3)Oracle针对创建的视图,只在数据字典中存储其定义。
输入并执行如下的语句查看创建的视图定义:
select text from user_views
where view_name=UPPER('emp_dept');
(4)查看视图各个列是否允许更新。
col owner format a20
col table_name format a20
col column_name format a20
select *
from user_updatable_columns
where table_name=UPPER('emp_dept');。