• mysql——视图——示例


    1、创建视图

    视图可以建立在一张表上,也可以建立在多张表上

    语法格式:


    create [ algorithm = { undefined | merge | temptable} ]

              view 视图名 [(属性清单)]

              as select 语句

             [ with { cascaded | loocal } check option ];

    ===========================================================

    前期表准备:

    create table employee ( num int(50),
                            d_id int(50),
                            name varchar(50),
                            age int(50),
                            sex varchar(50),
                            homeadd varchar(50)
                           );
    
    insert into employee values(1,1001,'zhangsan',26,'nan','beijing');
    insert into employee values(2,1001,'lisi',24,'nv','hunan');
    insert into employee values(3,1002,'wangwu',25,'nan','jiangsu');
    insert into employee values(4,1004,'aric',15,'nan','yingguo');
    
    select * from employee;
    
    create table department ( d_id int(50),
                              d_name varchar(50),
                              functione varchar(50),
                              address varchar(50)
                            );
    
    insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao');
    insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng');
    insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating');
    
    select * from department;

    select * from employee;

    select * from department;

    ==================================================

    在单表上创建视图:

    /*在单表上创建视图*/
    
    create view department_view1 as select * from department;         /*创建视图*/
    desc department_view1; /*查看视图*/
    select * from department_view1; select d_id,d_name,functione from department_view1;

     

     

    =======================================================

    单表创建视图,示例02:

    create view department_view2(name2,functione2,location2) as select d_name,functione,address from department;
    
    desc department_view2;
    
    select * from department_view2;

    =======================================================

    在多表上创建视图:

    create view department_view3(d_id,name,age,sex,homeadd,bmmc,gongn,bangonglouceng) as 
    select employee.d_id,employee.name,employee.age,employee.sex,employee.homeadd,department.d_name,department.functione,department.address 
    from employee,department where employee.d_id = department.d_id;
    describe department_view3;
    select * from department_view3; 

    ===================================================================

    ===================================================================

    查看视图

              describe 视图名称;

    或者

             desc 视图名称;

    describe department_view1;
    desc department_view1;

    ================================================

    ================================================

    查看视图基本信息

    show table status like '视图名';

    show table status like 'department_view1';

    ========================================================

    ========================================================

    查看视图详细信息

    show create view 视图名;

    show create view department_view1;

    ================================================================

    ===============================================================

    在views表中查看视图详细信息

    select * from information_schema.views;

    ====================================================================

    =====================================================================

    修改视图
    
    修改视图是指修改数据库中已经存在的表的定义。
    
    当基本表的某些字段发生改变时,可以通过修改视图来保持视图和基本表之间的一致;
    
    (1)create or replace view语句修改视图
    
    语法格式:
               create or replace [ algorithm = { undefined | merge | temptable} ]
    
                          view 视图名 [(属性清单)]
    
                          as select 语句
    
                         [ with { cascaded | loocal } check option ];
    
    
    
    (2)alter语句修改视图
    
    
    语法格式:
               alter [ algorithm = { undefined | merge | temptable} ]
    
                      view 视图名 [(属性清单)]
    
                      as select 语句
    
                      [ with { cascaded | loocal } check option ];

    -----------------------------------------------------------------------------------------------------------------------

    修改视图:

    使用 create or replace 语句,后面的语法格式都一样。

    在视图已经存在的情况下,可以对视图进行修改;

    在视图不存在的情况下,可以创建视图

    select * from department_view2;

    create or replace view department_view2(id,name2,functione2,location2) as select d_id,d_name,functione,address from department;

    desc department_view2;

    select * from department_view2;

    使用alter语句也可以修改视图,后面其它语法格式一样;

    alter语句修改视图:

    
    语法格式:
               alter [ algorithm = { undefined | merge | temptable} ]
    
                      view 视图名 [(属性清单)]
    
                      as select 语句
    
                      [ with { cascaded | loocal } check option ];

    --------------------------------------------------------------------------------------------------------------
    select * from department_view2;

    alter view department_view2(functione2,location2) as select functione,address from department;

    desc department_view2;

    select * from department_view2;

    ===================================================================

    ===================================================================

    更新视图,即更新原始表格中的数据,但是只能在权利范围之内,一般不建议使用。

    更新视图指:通过视图来插入(insert)、更新(update)、删除(delete)表中的数据。

    因为视图是一个虚拟表,其中没有数据。

    通过视图更新时,都是转换到基本表来更新。

    更新视图时,只能更新权限范围内的数据,超出范围就不能更新。

    --------------------------------------------------------------------------------------------------------------------

    注意:并不是所有的视图都可以更新的,以下几种情况不能更新视图;

    (1)、视图中包含sum()、count()、max()和min()等函数;

    (2)、视图中包含union、union all、distinct、group by、having等关键字;

    (3)、常量视图;

    (4)、视图中的select中包含子查询;

    (5)、由不可更新的视图导出的视图;

    (6)、创建视图时,algorithm为temptable类型;temptable类型是临时表类型。系统默认临时表示不能更新的;

    (7)、视图对应的表上存在没有默认值的列,而且该列没有包含在视图里。

    除了上述条件,[ with { cascaded | loocal } check option ];也将决定视图能否更新。

    local参数表示更新视图时,要满足该视图本身定义的条件即可;

    cascaded参数表示更新视图时,要满足所有相关视图和表的条件;

    没有指明时,默认为cascaded参数;

    -------------------------------------------------------------------------------------------------------------------------

    执行前查询:

     

    执行更新:

     

    执行后查询:

     

    =========================================================================

    删除视图:

    删除视图是指删除数据库中已经存在的视图的定义;删除视图时,只能删除视图的定义,不会删除数据
    
    语法格式:drop view [if exists] 视图名列表;
    
    if exists参数指判断视图是否存在,如果存在则执行,否则不执行,
    
    视图名列表参数表示要删除的视图的名称列表,各个视图名称之间用逗号隔开;

  • 相关阅读:
    [CSP-S模拟测试]:迷宫(最短路)
    [CSP-S模拟测试]:五子棋(模拟)
    [CSP-S模拟测试]:点亮(状压DP+树上背包DP)
    [CSP-S模拟测试]:统计(树状数组+乱搞)
    [CSP-S模拟测试]:组合(欧拉路)
    [CSP-S模拟测试]:笨小猴(随机化)
    最小表示法
    BZOJ4868 [Shoi2017]期末考试 【三分 + 贪心】
    BZOJ4870 [Shoi2017]组合数问题 【组合数 + 矩乘】
    BZOJ4919 [Lydsy1706月赛]大根堆 【dp + 启发式合并】
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12093699.html
Copyright © 2020-2023  润新知