• MySQL视图及索引


    视图

    视图就是一个表或多个表的查询结果,它是一张虚拟的表,因为它并不能存储数据。

    视图的作用、优点:

      限制对数据的访问

      让复杂查询变得简单

      提供数据的独立性

      可以完成对相同数据的不同显示

    //创建、修改视图
    create or replace view view_temp
    as
    select name, age from temp;
    
    //通常不对视图的数据做修改操作,因为视图是一张虚拟的表,它并不存储实际数据。如果想让视图不被修改,可以用with check option来完成限制。
    create or replace view view_temp
    as 
    select *from temp
    with check option;
    
    //删除视图
    drop view view_temp;
    
    //显示创建语法
    show create view v_temp;

    显示学生成绩单的视图

    mysql> create view student_cj as select students.number,students.name,course.math,course.english,course.chinese
        -> from students,course
        -> where students.number=course.number;
    Query OK, 0 rows affected (0.00 sec)
    

    使用student_cj个视图,显示结果

    删除这个视图

    mysql> drop view student_cj;
    Query OK, 0 rows affected (0.00 sec)
    

    查看视图的信息

    mysql> show create view student_cjG

    索引

    1.在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据。[ 是为了快速查询而针对某些字段建立起来的。]
    2.更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。
    3.表

    01.数据库中的数据都是存储在表中的
    02.表是物理存储的,真实存在的

    2.案例

    创建索引:create index degree_fast on score (degree); 
    这里的dgeree_fast是索引名,score是表明,degree是表中的一个字段。 
    创建视图:

    create view [viewName] as 
    select [someFields]
    from [tableName]

    删除索引: 
    - 01.方式一:drop index degree_fast on score; 
    - 02.方式二:alter table score drop index degree_fast;

    Mysql cannot drop index needed in a foreign key constraint.Mysql不能在外键约束下删除索引。如果有外键的话,需要先把外键删除,然后再删除索引。

  • 相关阅读:
    ARM 64位系统下运行32位程序
    CMakeFiles示例
    Linux c++ 试验10 一例undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'”
    EclipseC++学习笔记9 将文件从项目中排除与恢复
    WSL 一例运行时提示access denied解决办法
    arm64环境搭建2 几个小tip
    飞凌FCU2201 使用2 设置wifi sta模式
    minicom退出
    linux出现TIME_WAIT的原因
    模板类出现 undefined reference 错误
  • 原文地址:https://www.cnblogs.com/heian99/p/11972286.html
Copyright © 2020-2023  润新知