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;
相关推荐
倔强的石头_4 小时前
事务边界与批量写入:避免长事务、锁等待和日志压力
数据库
努力努力再努力wz4 小时前
【Redis入门系列】从 KEYS 到 SCAN:渐进式遍历、Cursor 与位反转原理
数据库·redis·缓存
代码的小搬运工5 小时前
KVO学习
学习·ios·cocoa
坐吃山猪5 小时前
【多线程】Lock与Condition
大数据·数据库
辣知6 小时前
辣知·化智47 黄帝陵与民族精神归途
学习
Lightpwd6 小时前
Spring Boot 多数据源落地:AbstractRoutingDataSource + 注解切面(附源码)
数据库·后端
知识分享小能手6 小时前
深度学习学习教程,从入门到精通,数值计算 — 知识点详解(4)
人工智能·深度学习·学习
LabVIEW开发6 小时前
LabVIEW 64位安装的位深陷阱:工具包、内存与工程兼容
数据库·labview·labview知识·labview功能·labview程序
寺中人6 小时前
MySQL 8.0 Windows 完整安装教程:环境配置、密码重置与常见报错排查
数据库·windows·mysql·环境搭建·mysql 安装
这个DBA有点耶6 小时前
同样48核配置TPS差1倍?高性价比数据库一体机的“软硬协同”才是分水岭
服务器·数据库·架构