当前位置: 首页 > article >正文

MySQL学习(视图总结)

文章目录

  • MySQL的视图
  • 视图基本操作
    • 创建视图
    • 修改视图
  • 练习

MySQL的视图

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

视图基本操作

创建视图

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

/*
    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
*/
create or replace view v_emp
as
select a.deptno, a.ename, a.sal from emp a;
  • 查看表与视图
show full tables;

修改视图

  • 更换构造视图的select查询语句
alter view v_emp
as
select a.deptno, a.ename, b.dname from emp a, dept b where a.deptno = b.deptno;
  • 更新视图(针对更换查询语句前)
update v_emp set ename = 'jack' where ename = 'scott';
insert into v_emp values(10, 'jack', 5000);
  • 下面情况不可更新
  1. 视图包含聚合函数
        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
        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
        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
        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. 视图包含子查询
        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
        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仅引用文字值
        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. 重命名视图
rename table v_emp to new_v_emp;
  1. 删除视图
drop view if exists new_v_emp;

练习

  • 找出平均工资最高的部门
    – 多重子查询
        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;

– 视图

        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;
  • 找出工资比领导高的员工
        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的员工
        -- 工资在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;

http://www.kler.cn/news/307019.html

相关文章:

  • idea集成和使用Git指南
  • uni-app 应用名称 跟随系统语言 改变
  • 沉浸式体验和评测Meta最新超级大语言模型405B
  • 【南方科技大学】CS315 Computer Security 【Lab2 Buffer Overflow】
  • JAVA基础面试题总结(十五)——设计模式
  • USB摄像头视频流转RTSP流
  • 算法题解:斐波那契数列(C语言)
  • OpenAI / GPT-4o:Python 返回结构化 / JSON 输出
  • PyTorch----模型运维与实战
  • C/C++内存管理——内存泄漏/内存碎片
  • Day20笔记-面向对象类和对象类中的属性和函数构造和析构函数
  • ASP.NET Core 入门教学二十五 集成vue3
  • 【PHP代码审计】 PHP环境搭建
  • 【Python机器学习】序列到序列建模——使用序列到序列网络构建一个聊天机器人
  • CSP-J 算法基础 图论
  • springboot3.X版本集成mybatis遇到的问题
  • Day16笔记-匿名函数应用闭包变量的作用域迭代器和生成器
  • VSCode中配置C/C++环境
  • python基础 --- 爬虫前篇
  • NumPy 线性代数
  • 【60天备战软考高级系统架构设计师——第二十天:运维与服务管理——服务管理与优化】
  • 用Python打造互动式中秋节庆祝小程序
  • Qt 基础按钮布局管理
  • flink的窗口
  • QT QSystemTrayIcon创建系统托盘区图标失败
  • 后端开发刷题 | 兑换零钱(动态规划)
  • Prometheus+grafana+kafka_exporter监控kafka运行情况
  • 【Scala入门学习】基本数据类型和变量声明
  • [Mamba_4]LMa-UNet
  • 95、k8s之rancher可视化