• mysql数据库(9)--视图


    1、什么是视图?

    视图是一种虚拟存在的表格,行和列的数据在使用视图时动态生成的,只保存了sql逻辑,不保存查询结果。(可以实现sql语句的复用)

     1 # 查询姓张的学生名和专业名
     2 
     3 # 创建视图
     4 create view v1
     5 as
     6 select stuname, majorname
     7 from stuinfo as a
     8 inner join major as m 
     9 on s. majorid = m.id
    10 
    11 # 视图的使用
    12 select * from v1 where stuname like '张%'

    2、应用场景

    • 多个地方用到同样的查询结果
    • 该查询结果使用的sql语句比较复杂

    3、视图的创建

    1 create view 视图名
    2 as
    3 查询语句

    4、视图的修改

    方式一

    1 create or replace view 视图名称
    2 as
    3 查询语句

    方式二

    1 alter view 视图名
    2 as
    3 查询语句

    5、视图的删除

    1 drop view 视图名,视图名,...

    6、查看视图

    1 desc 视图名   # 方式一
    2 
    3 show create view 视图名    # 方式二

    7、视图的更新

    视图进行增删改操作之后,原始表中的数据也会相应的发生改变,但是具备以下特点的视图,不能进行更新

    (1)包含分组函数 

    (2)常量视图

    (3)包含子查询

    (4)包含join语句

    (5)from一个不能更新的视图

    (6)where子句的子查询引用了from子句中的表

    1 select last_name, email, salary
    2 from employee
    3 where employee_id in(
    4     select manager_id
    5     from employee
    6     where manager_id is not null
    7 )

    8、视图的优点

    (1)重用sql语句

    (2)简化复杂的sql操作,不需要知道查询细节

    (3)保护数据,提高安全性

  • 相关阅读:
    微软外服 AlI In One
    js 循环多次和循环一次的时间的性能对比 All In One
    vue inject All In One
    Excel 表格数据倒置 All In One
    SVG tickets All In One
    OH MY ZSH All In One
    js array for loop performance compare All In One
    mac terminal show You have new mail All In one
    新闻视频 26 制作母版页
    转自牛腩 母版页和相对路径
  • 原文地址:https://www.cnblogs.com/yif930916/p/15036751.html
Copyright © 2020-2023  润新知