• 视图



    视图

    一、MySQL视图

    1.1.视图的含义

    • 视图是一个虚拟表,是从数据库中一个或是多个表导出来的表。视图也可以从已经存在的视图基础上定义。
    • 单表视图可以进行增、删、改。对视图的操作语法和操作表一致。
    • 联表视图无法进行DML

    1.1.1 视图基本操作:

    • 查看MySQL所有的视图
    mysql> show table status where comment='view'G
    • 创建视图
    mysql> create ALGORITHM=[MERGE/TEMPTABLE/UNDEFINED] 
    view view_name  as  (select * from test);
    或是:
    mysql> create view view_name(v_id,v_class) as (select s_id,class from stu_info);

    补充:

    • 视图的ALGORITHM
    • ALGORITHM = MERGE/TEMPTABLE/UNDEFINED

        MERGE:当使用视图时,会把查询视图的语句和创建视图的语句合并起来,形成一条语句,最后再从基表中查询

        TEMPTABLE:当使用视图时,会把创建视图的语句的查询结果当成一张临时表,再从临时表中进行筛选

        UNDEFINED:未定义,自动,让系统帮你选

    • 视图的检查:
    mysql> show table status like 'view_name'G
    或是:
    mysql> desc [describe]  view_name;
    • 视图的修改:
    mysql> alter view view_name as (select * from test01);
    或是:
    mysql> create or replace view view_name as (select * from test);
    • 视图的删除:
    mysql> drop view view_name;

    1.2.视图案例

    • 有两张基本表:studentstu_info,按要求只公布id号、姓名和班级。

    表结构如下:

    字段1

    字段2

    字段3

    student

    s_id

    name

    ——

    stu_info

    s_id

    class

    addr

    1.2.1.创建表和数据

    mysql> create table student ( s_id int, name varchar(40) );
    mysql> insert into student values(1,'tansk'), (2,'tanmy');
    mysql> create table stu_info( s_id int, class  varchar(40), addr varchar(90));
    mysql> insert into stu_info values('001','1011','gz'),(002,'1012','nn');
    
    查看表数据:
    mysql
    > select * from student; +------+-------+ | s_id | name | +------+-------+ | 1 | tansk | | 2 | tanmy | +------+-------+ 2 rows in set (0.00 sec) mysql> select * from stu_info; +------+-------+------+ | s_id | class | addr | +------+-------+------+ | 1 | 1011 | gz | | 2 | 1012 | nn | +------+-------+------+ 2 rows in set (0.00 sec) mysql>

    1.2.2.编写查询SQL

    注意:一般的都是先写好查询SQL,在进行视图创建。因为视图中引用的就是这一条SQL

    方法一(普通查询):

    mysql> select a.s_id,a.name, b.class from student as a,stu_info as b where a.s_id=b.s_id;
    +------+-------+-------+
    | s_id | name  | class |
    +------+-------+-------+
    |    1 | tansk | 1011  |
    |    2 | tanmy | 1012  |
    +------+-------+-------+
    2 rows in set (0.04 sec)
    
    mysql> 

    方法二(外连接,推荐使用):

    mysql> select a.s_id,a.name, b.class from student as a left join stu_info as b on a.s_id=b.s_id ;
    +------+-------+-------+
    | s_id | name  | class |
    +------+-------+-------+
    |    1 | tansk | 1011  |
    |    2 | tanmy | 1012  |
    +------+-------+-------+
    2 rows in set (0.00 sec)
    
    mysql> 

    1.2.3.创建视图

    mysql> create view  view_stuAndinfo_01  as  ( select a.s_id,a.name, b.class from student as a,stu_info as b where a.s_id=b.s_id); 
    或是:
    mysql> create view  view_stuAndinfo_02  as  (select a.s_id,a.name, b.class from student as a left join stu_info as b on a.s_id=b.s_id);

    查询视图:

    mysql>  select * from  view_stuAndinfo_01;       
    +------+-------+-------+
    | s_id | name  | class |
    +------+-------+-------+
    |    1 | tansk | 1011  |
    |    2 | tanmy | 1012  |
    +------+-------+-------+
    2 rows in set (0.03 sec)
    
    mysql>  select * from  view_stuAndinfo_02;
    +------+-------+-------+
    | s_id | name  | class |
    +------+-------+-------+
    |    1 | tansk | 1011  |
    |    2 | tanmy | 1012  |
    +------+-------+-------+
    2 rows in set (0.00 sec)
    
    mysql> 

    1.2.4.查看视图属性

    mysql> show table status like 'view_stuAndinfo_01'G
    *************************** 1. row ***************************
               Name: view_stuAndinfo_01
             Engine: NULL
            Version: NULL
         Row_format: NULL
               Rows: NULL
     Avg_row_length: NULL
        Data_length: NULL
    Max_data_length: NULL
       Index_length: NULL
          Data_free: NULL
     Auto_increment: NULL
        Create_time: NULL
        Update_time: NULL
         Check_time: NULL
          Collation: NULL
           Checksum: NULL
     Create_options: NULL
            Comment: VIEW
    1 row in set (0.00 sec)
    
    mysql>

    1.3 视图的作用

    • 提高了重用性,就和函数一样 --当频繁获取一些数据的时候,减少了写SQL的过程
    • 提高了安全性。对不同的用户创建不同的视图,每个用户跟进可以看到的信息不同
    • 简化查询,提高了查询性能
    • 提高灵活性,在生产中如果修改表结构导致工作量比较大,可以使用视图(虚拟表)的形式达到修改表结构的效果。
    水果大佬
  • 相关阅读:
    google浏览器高清壁纸保存
    vmworkstation安装unbuntu server 网络配置:NAT模式
    python量化交易相关资料
    Oracle VM VirtualBox启动后莫名奇妙的报错
    oracle RAC 跨网段客户端访问 报ORA-12170
    odoo开发 相关知识点
    C#.ToString()格式大全
    flex简单参考实例
    NPOI读写Excel
    C# Stream 和 byte[] 之间的转换(文件流的应用)
  • 原文地址:https://www.cnblogs.com/tanshouke/p/12360174.html
Copyright © 2020-2023  润新知