MySQL学习(视图总结)

文章目录

MySQL的视图

  • 视图是虚拟的表,是从数据库中一个或多个表中导出来的表,作用是可以隐藏一些数据,也可以将一些复杂的查询结果做成视图。
  • 数据库只保存视图的定义,而不保存视图中的数据,数据存放在原表也依赖于原表,当原表发生变化时,视图中的数据也会发生变化。
  • 好处
    -- 视图可以简化用户的数据查询操作,可以把重复使用的查询更加方便使用,也可以使复杂的查询易于理解和使用。
    -- 视图可以保护数据的安全,可以对不同的用户定义不同的查询结果。

视图基本操作

创建视图

将一条select语句封装成一个虚拟表

sql 复制代码
/*
    create [or replace] [algorithm={undefined|merge|temptable}]
    view 视图名称 [(column_list)]
    as select语句
    [with [cascasded | local] check option]

    algrithm: 视图算法,默认是undefined,可选merge或temptable
    column_list: 指定视图中各个属性的名词,默认与select中的查询属性相同
    [with [cascasded | local] check option]:指定视图是否允许更新,默认是cascasded
*/
sql 复制代码
create or replace view v_emp
as
select a.deptno, a.ename, a.sal from emp a;
  • 查看表与视图
sql 复制代码
show full tables;

修改视图

  • 更换构造视图的select查询语句
sql 复制代码
alter view v_emp
as
select a.deptno, a.ename, b.dname from emp a, dept b where a.deptno = b.deptno;
  • 更新视图(针对更换查询语句前)
sql 复制代码
update v_emp set ename = 'jack' where ename = 'scott';
insert into v_emp values(10, 'jack', 5000);
  • 下面情况不可更新
  1. 视图包含聚合函数
sql 复制代码
        create or replace view v_emp_b1
        as
        select count(*) cnt from emp;
        select * from v_emp_b1;
        update v_emp_b1 set cnt = 20;   -- 报错
        insert into v_emp_b1 values(20); -- 报错
  1. 视图包含distinct
sql 复制代码
        create or replace view v_emp_b2
        as
        select distinct job from emp;
        select * from v_emp_b2;
        update v_emp_b2 set job = 'fff' where job = 'analyst'; -- 报错
        insert into v_emp_b2 values('fff'); -- 报错
  1. 视图包含分组函数(group by)或having
sql 复制代码
        create or replace view v_emp_b3
        as
        select a.deptno, count(*) cnt from emp a group by a.deptno having count(*) > 2;
        select * from v_emp_b3;
        update v_emp_b3 set cnt = 20 where cnt = 5; -- 报错
        insert into v_emp_b3 values(10, 20); -- 报错
  1. 视图包含union或union all
sql 复制代码
        create or replace view v_emp_b4
        as
        select a.deptno, a.ename from emp a where a.deptno = 10
        union
        select a.deptno, a.ename from emp a where a.deptno <= 20;
        select * from v_emp_b4;
        update v_emp_b4 set ename = 'jack' where ename = 'scott'; -- 报错
        insert into v_emp_b4 values(10, 'jack'); -- 报错
  1. 视图包含子查询
sql 复制代码
        create or replace view v_emp_b5
        as
        select a.deptno, a.ename from emp a where a.sal > (select avg(a.sal) from emp a);
        select * from v_emp_b5;
        update v_emp_b5 set ename = 'ham' where ename = 'king'; -- 报错
        insert into v_emp_b5 values(10, 'ham'); -- 报错
  1. 视图包含join
sql 复制代码
        create or replace view v_emp_b6
        as
        select a.deptno, b.dname from emp a join dept b on a.deptno = b.deptno;
        select * from v_emp_b6;
        update v_emp_b6 set dname = 'sales' where dname = 'accounting'; -- 报错
        insert into v_emp_b6 values(10, 'sales'); -- 报错
  1. select仅引用文字值
sql 复制代码
        create or replace view v_emp_b7
        as
        select 'aaa' dname, '小星星' ename;
        select * from v_emp_b7;
        update v_emp_b7 set dname = 'bbb' where ename = '小星星'; -- 报错
        insert into v_emp_b7 values('bbb', '小星星'); -- 报错
  • 视图其他操作
  1. 重命名视图
sql 复制代码
rename table v_emp to new_v_emp;
  1. 删除视图
sql 复制代码
drop view if exists new_v_emp;

练习

  • 找出平均工资最高的部门
    -- 多重子查询
sql 复制代码
        select
            a.dname, a.deptno
        from
            dept a join
        (
        select
            * from
        (
        select
        *,
        rank() over(order by t.avg_sal DESC) rn
        from
        (
        select a.deptno, avg(a.sal) avg_sal from emp a group by a.deptno
        ) t
        ) tt where tt.rn = 1
        ) ttt where a.deptno = ttt.deptno;

-- 视图

sql 复制代码
        create or replace view view_avg_sal1
        as
        select a.deptno, avg(a.sal) avg_sal from emp a group by a.deptno;
        select * from view_avg_sal1;

        create or replace view view_avg_sal2
        as
        select *, rank() over(order by t.avg_sal DESC) rn from view_avg_sal1 t;
        select * from view_avg_sal2;

        create or replace view view_avg_sal3
        as
        select * from view_avg_sal2 where rn = 1;
        select * from view_avg_sal3;

        select a.dname, a.deptno from dept a join view_avg_sal3 b on a.deptno = b.deptno;
  • 找出工资比领导高的员工
sql 复制代码
        create or replace view v_emp_b8
        as
        select a.deptno _deptno, b.ename _yg_name, a.empno _ld_id from emp a, emp b where a.empno = b.mgr and a.sal < b.sal;
        select * from v_emp_b8;

        select a.dname, b._yg_name, b._ld_id from dept a join v_emp_b8 b on a.deptno = b._deptno;
  • 找出工资在4级,且入职时间早于1985年的且工资最高的且工作地点是dallas的员工
sql 复制代码
        -- 工资在4级的员工信息
        create or replace view v_emp_b9
        as
        select * from emp a, salgrade b where a.sal between b.losal and b.hisal and b.grade = 4;
        select * from v_emp_b9;

        -- 入职时间早于1985年的员工信息
        create or replace view v_emp_b10
        as
        select * from v_emp_b9 a where year(a.hiredate) < 1985;
        select * from v_emp_b10;
        -- 工作地点是dallas并按工资降序
        create or replace view v_emp_b11
        as
        select a.empno, a.ename, a.sal from v_emp_b10 a, dept b where a.deptno = b.deptno and b.loc = 'dallas' order by a.sal desc;
        select * from v_emp_b11;
        -- 窗函数得到工资序号
        create or replace view v_emp_b12
        as
        select *, rank() over (order by a.sal desc) rn from v_emp_b11 a;

        select * from v_emp_b12 where rn = 1;
相关推荐
DREAM依旧17 分钟前
MySQL数据库概述与基础
mysql·database
尘浮生28 分钟前
Java项目实战II基于Java+Spring Boot+MySQL的大型商场应急预案管理系统(源码+数据库+文档)
java·开发语言·数据库·spring boot·spring·maven·intellij-idea
问道飞鱼1 小时前
每日学习一个数据结构-B+树
数据结构·b树·学习
MXsoft6181 小时前
监控易监测对象及指标之:全面监控DB2_linux数据库
数据库·oracle
尘浮生1 小时前
Java项目实战II基于Java+Spring Boot+MySQL的校园社团信息管理系统(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·spring·maven
petaexpress1 小时前
分布式云化数据库的优缺点分析
数据库·分布式
不染_是非1 小时前
Django学习实战篇六(适合略有基础的新手小白学习)(从0开发项目)
后端·python·学习·django
失心疯_20232 小时前
Mysql_使用简介
数据库·sql·mysql·关系型数据库·ddl·dml·mysql教程
小威要向诸佬学习呀2 小时前
MySQL中的LIMIT与ORDER BY关键字详解
数据库·mysql
Midsummer啦啦啦2 小时前
NumPy库学习之argmax函数
学习·numpy