zoukankan      html  css  js  c++  java
  • MySQL 索引、视图

    1、索引

    • 什么是索引
      • 一个索引是存储在表中的数据结构,索引在表的列名上创建。索引中包含了一个列的值,这些值保存在一个数据结构中
    • 索引优缺点
      • 索引大大提高了查询速度
      • 会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件
    • 普通索引
      • 普通索引是最基本的索引类型,没有任何限制,值可以为空,仅加速查询。普通索引是可以重复的,一个表中可以有多个普通索引
      • 创建索引
        • create index 索引名称 on 表名(字段);
      • 查看索引
        • show index from 表名;
      • 删除索引
        • drop index 索引名称 on 表名;
    # 给 name 字段创建索引 aa
    mysql> create index aa on t_student(name);
    
    # 查看索引
    mysql> show index from t_student;
    
    +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-----------+------------+----------+--------------+-------------+-----------+---
    | t_student |          1 | aa       |            1 | name        | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |
    +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    # 删除索引 aa
    mysql> drop index aa on t_student;
    • 唯一索引
      • 它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值
      • 创建索引
        • create unique index 索引名称 on 表名(字段)
    # 给 name 字段创建唯一索引
    mysql> create unique index bb on t_student(name);
    
    mysql> show index from t_student;
    
    +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | t_student |          0 | bb       |            1 | name        | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |
    +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    • 主键索引
      • 主键索引是一种特殊的唯一索引,一个表只能有一个主键,不允许有空值;索引列的所有值都只能出现一次,即必须唯一。简单来说:主键索引是加速查询 + 列值唯一(不可以有null)+ 表中只有一个
      • 主键是一种唯一性索引,但它必须指定为 "PRIMARY KEY"
      • 每个表只有一个主键
      • 创建索引
        • alter table 表名 add primary key(字段)
    # 给 score 字段添加主键索引
    mysql> alter table t_course add primary key(score);
    
    mysql> show index from t_course;
    
    +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | t_course |          0 | PRIMARY  |            1 | score       | A         |           6 |     NULL | NULL   |      | BTREE      |         |
    +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

    2、视图

    • 视图的特点
      • 视图时由基本表产生的虚表
      • 视图的创建和删除不影响基础表
      • 视图的更新和删除直接影响基础表
      • 基础表的更新和删除直接影响视图  
    • 视图的作用
      • 数据库中数据的查询非常复杂,例如多表,子查询,编写语句较多,并且这种查询常被重复使用,那么我们就可以创建视图,避免每次写 sql 语句会错误,也提高了查询的效率
      • 为了安全,在公司中,有些字段为保密字段,那么可以创建视图,限制对某些字段的操作。 
    • 创建视图
      • create view 视图名 as (查询 sql 语句)
    # 创建视图 v_stu
    mysql> create view v_stu as (select id,name,age from t_student);
    • 显示目前有哪些视图
      • show tables;
      • 注意点创建的视图都在表的最下面
    # 查看创建的视图 v_stu
    mysql> show tables;
    
    +-------------------+
    | Tables_in_student |
    +-------------------+
    | t_course          |
    | t_student         |
    | v_stu             |
    +-------------------+
    
    mysql> select * from v_stu;
    +----+----------+------+
    | id | name     | age  |
    +----+----------+------+
    |  1 | zhangsan |   18 |
    |  2 | wangwu   |   20 |
    |  3 | zhaoliu  |   19 |
    |  4 | lisi     |   22 |
    +----+----------+------+
    • 修改视图
      • alter view 视图名 as (查询语句)
    mysql> alter view v_stu as (select id,name from t_student);
    
    mysql> select * from v_stu;
    +----+----------+
    | id | name     |
    +----+----------+
    |  4 | lisi     |
    |  2 | wangwu   |
    |  1 | zhangsan |
    |  3 | zhaoliu  |
    +----+----------+
    •  删除视图
      • drop view 视图名
    # 删除视图 v_cou
    mysql> drop view v_cou;
    
    mysql> show tables;
    +-------------------+
    | Tables_in_student |
    +-------------------+
    | t_course          |
    | t_student         |
    | v_stu             |
    +-------------------+
  • 相关阅读:
    swagger-ui 系统配置过程(基于spring+springmvc+swagger+springfox配置 web-api 管理系统)
    如何简单扫描整理
    C#双面打印解决方法(打印wordexcel图片)
    MODI出现ORC RUNNING ERROR的解决方法
    EMGU 2.9.X在VS2010下调试真正靠谱的配置
    如何解决The underlying provider failed on Open问题
    shell编程之——cat /dev/null作用
    docker 容器的设置2
    docker 容器的设置1
    ssl证书生成与转换(pfx, pem, key, crt)
  • 原文地址:https://www.cnblogs.com/ZhengYing0813/p/12675579.html
Copyright © 2011-2022 走看看