一、视图
(1)创建v_dept视图包含t_dept表的全部列
CREATEVIEW v_dept
AS
SELECT deptno,dname,loc
FROM t_dept
(2)创建v_employee视图包含包含t_employee表员工编号、姓名和收入,并指定此视图的列为v_no,v_name,v_sal
CREATEVIEW v_employee
AS
SELECT empno v_no,enamev_name,sal v_sal
FROMt_employee
(3)从v_dept视图中查询出部门号为20的部门名称
SELECTdname
FROMv_dept
WHERE deptno=20
(4)从v_employee视图查询出姓名为“JONES”的员工的收入
SELECTv_SAL
FROMv_employee
WHERE v_name = 'JONES'
(5)创建v_emp_dep视图,包含员工编号、姓名、收入和部门名称
CREATEVIEW v_emp_dep
AS
SELECTE.empno, E.ename, E.sal,D. dname
FROMt_employee E
INNERJOIN t_dept D ON D.deptno = E.deptno
(6)创建v_deptCount视图,包含部门名称,部门人数和此部门的平均薪资
CREATE VIEW v_deptCount
AS
SELECTD.dname,COUNT(1),AVG(sal)
FROMt_dept D
INNERJOIN t_employee E ON E.deptno = D.deptno
GROUPBY E.deptno
(7)向视图v_dept中插入一条部门数据:80,研发部,张家界
INSERTINTO v_dept (deptno,dname,loc)
VALUES(80,'研发部','张家界');
(8)修改v_employee视图中编号为7788的雇员的姓名为“Dilraba”
UPDATEv_employee
SET v_name = 'Dilraba'
WHEREv_no = 7788
(9)删除v_employee视图中编号为7900的雇员信息
DELETEFROM v_employee
WHEREv_no = 7900
(10)查看v_emp_dep视图定义信息的命令
SHOWCREATE VIEW v_deptcount
(11)查看v_deptCount视图的设计信息
DESCv_emp_dep
(12)删除视图 v_dept
DROPVIEW v_dept
二、触发器
(1)创建UPDATE触发器,当t_dept表中部门号发生变化时,t_employee表中员工所属部门号也将改变
CREATETRIGGER tri_upd_dep
BEFOREUPDATE
ONt_dept FOR EACH ROW
UPDATE t_employee SETdeptno=new.deptno where deptno=old.deptno;
UPDATEt_dept SET deptno=30 where deptno=1;
(2)创建INSERT触发器,向t_employee插入员工编号、名字,自动将当前日期插入到hiredate列中,并将操作信息插入到t_diary日志表中(t_diary表参考P140 图8.3)
CREATETABLE t_diary(
diaryno int auto_increment,
tablenameVARCHAR(20),
diarytimedatetime,
PRIMARYkey(diaryno)
);
CREATETRIGGER tri_insert_emp
AFTERINSERT
ONt_employee FOR EACH ROW
INSERTINTO t_diary VALUES(NULL,'t_employee',NOW());
INSERT INTOt_employee(empno,ename,hiredate) values(10086,'zxb',NOW());
(3)查看(1)创建的触发器
SHOWTRIGGER;
(4)删除(2)创建的触发器
DROP TRIGGERtri_insert_emp;