---------------练习1-------------------------------------------------------------------------------------------------
1.创建一个视图,通过该视图可以查询到工资在2000-5000内并且姓名中包含有A的员工编号,姓名,工资。
create view view_emp as
select empno,ename,sal from emp ------创建view视图权限不足,登录sys-commad window输入命令:grant create view to scott授权
where sal between 2000 and 5000;
2.通过上述创建的视图查询数据
select *from view_emp ;
----------------练习2-------------------------------------------------------------------------------------------------
1.创建一个视图,通过该视图可以查询到工作在NEW YORK和CHICAGO的员工编号,姓名,部门编号,入职日期。
create view view_emp_dept as
select e.empno,e.ename,e.deptno,e.hiredate from emp e,dept d
where e.deptno=d.deptno and loc in(‘NEW YORK’,‘CHICAGO’) ;
select *from view_emp_dept;
select *from dept;
2.创建一个视图,通过该视图可以查询到每个部门的部门名称及最低工资。
create view view_dept as
select d.dname,min(e.sal)最低工资 from dept d,emp e
where d.deptno=e.deptno
group by d.dname;
select *from view_dept
3.通过如上视图,查询每个部门工资最低的员工姓名及部门名称
select e.ename,d.dname
from emp e,dept d,view_dept vp
where e.deptno=d.deptno and d.dname=vp.dname
and e.sal=vp.最低工资;
select *from emp;
----------------课后作业-------------------------------------------------------------------------------------------------
1.创建视图v_emp_21,包含20号部门的员工编号,姓名,年薪列(年薪=12*(工资+奖金);
create view v_emp_21 as
select empno,ename, 12*(nvl(sal,0)+nvl(comm,0))年薪 from emp
where deptno=20 ;
select *from v_emp_21
2.从视图v_emp_21中查询年薪大于1万元员工的信息;
select *
from emp e,v_emp_21 v21
where e.empno=v21.empno
and v21.年薪>10000;
3.请为工资大于2000的员工创建视图,要求显示员工的部门信息,职位信息,工作地点;
create view view_sal as
select e.deptno,e.job ,d.dname,d.loc from emp e ,dept d
where e.deptno=d.deptno and sal>2000;
select *from view_sal;
4.针对以上视图执行insert,update,delete,语句能否成功,为什么?
insert into view_sal
values (61,‘kol’,‘bumen’,‘shenzhen’);-----不成功,无法通过连接视图修改多个基表