二、操作题
1.创建视图emp_view2,包含员工编号,姓名,所在部门名称和收入。
mysql> create or replace view emp_view2
-> as
-> select e.员工编号,e.姓名,d.部门名称,s.收入
-> from employees e , departments d, salary s
-> where e.员工编号=s.员工编号 and e.员工部门号=d.部门编号;
Query OK, 0 rows affected (0.00 sec)
2.从emp_view2视图中查询研发部的员工编号,姓名,和收入。
mysql> select * from emp_view2 where 部门名称="研发部";
+--------------+-----------+--------------+---------+
| 员工编号 | 姓名 | 部门名称 | 收入 |
+--------------+-----------+--------------+---------+
| 302566 | 李玉珉 | 研发部 | 2980.7 |
| 308759 | 叶凡 | 研发部 | 2531.98 |
| 504209 | 陈林琳 | 研发部 | 2066.15 |
+--------------+-----------+--------------+---------+
3 rows in set (0.07 sec)
3.创建视图emp_view3,包含所有工作年限2年以上的员工编号,姓名,学历,出生日期,性别,工作年限及所在部门编号,在创建视图的时间加上with check option。
mysql> create or replace view emp_view3
-> as
-> select * from employees
-> where 工作年限 >2
-> with check option;
Query OK, 0 rows affected (0.00 sec)
4.从emp_view3视图中插入一条记录(041110,钟晓玲,博士,1973-12-01,男,3,4)。
mysql> insert into emp_view3 values ("041110","钟晓玲","博士","1973-12-01","男",3,null,null,4);
Query OK, 1 row affected (0.00 sec)
5.修改emp_view2,将李丽的收入增加200元。
mysql> update emp_view2
-> set 收入=收入+200
-> where 姓名="李丽";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6.删除视图emp_view3中本科学历的员工。
mysql> delete from emp_view3 where 学历="本科";
Query OK, 3 rows affected (0.00 sec)
7.修改视图emp_view3的定义,包含员工编号,姓名,学历,性别,出生日期。
mysql> alter view emp_view3
-> as select 员工编号,姓名,学历,性别,出生日期
-> from employees;
Query OK, 0 rows affected (0.00 sec)
8.删除视图emp_view2和emp_view3。
mysql> drop view emp_view2,emp_view3;
Query OK, 0 rows affected (0.00 sec)