zoukankan      html  css  js  c++  java
  • mysql 笔记l六

    索引分类

    • 普通索引
    • 唯一索引
    • 全文索引
    • 单列索引、多列索引
    • 组合索引

    索引的设计原则

    • 索引并非越多越好
    • 避免对经常更新的表进行过多的索引,并且索引列应尽可能少
    • 数据量小的表最好不要使用索引
    • 在条件表达式中经常会用到的不同值较多的列上要建立索引,在不同值少的列上不要建立索引
    • 当唯一性是某种数据本身的特征时,指定唯一索引
    • 在频繁进行排序或分组的列上建立索引,如果带排序的列有多个,可以在这些列上建立组合索引

    创建索引

    1. 在创建表时创建索引
    create table table_name
    (
        Column_definition,
        [unique | fulltext ] index | key [index_name] (col_name [(len)] [asc | desc] )
    );
    
    • unique 和 fulltext : 可选参数

      分别表示唯一索引和全文索引
      全文索引在 5.6 版本以后才支持

    • index 和 key

      为同义词,任选其一,作用相同

    • index_name

    • col_name :需要创建索引的 字段列

    • len

      索引长度,只有字符串类型的字段才能指定索引长度

    1. create index
    create [unique | fulltext] index index_name on table_name
    (col_name [(len)] [asc | desc],..... );
    
     create index index_id on data(name);
    
    1. alter table ... index
    alter table table_name add index | key  [index_name] 
    (col_name [(len)] [asc | desc],..... );
    
    alter table data add index index_num (number);
    

    查看索引

    • show index from table_name from databases
    • show index from databases.table_name

    删除索引

    • drop index index_name on table_name
    • alter table table_name drop index index_name

    视图

    • 视图是一个虚拟表,其内容由查询定义
    • 视图的作用

      简单性
      安全性
      逻辑独立性

    创建视图

        create [or replace] [algorithm = {undefined | merge | temptable }]
                view view_name [(column_list)]
                as select_statement
        [with [cascaded  | local ] check option ]
    
    • replace

      表示替换已经创建的视图,若视图不存在,则创建一个视图。

    • algorithm

      表示视图的算法,可选择 undefined , merge, temptable
      undefined 自动选择算法

      merge 将使用的视图语句与视图定义合并,使得视图定义的某一部分取代语句对应的部分

      temptable 将视图的结果存入临时表,然后用临时表来执行语句

    • view_name

    • column_list : 属性列

    • select_statement : 表示select语句

    • [with [cascaded | local ] check option ]

      表示视图在更新时保证在视图的操作权限范围之内

    • cascaded

      表示更新视图时要满足所有相关视图和表的条件

    • local

      表示更新视图时满足视图本身定义的条件

    • 创建视图时要注意

    1. 运行创建视图的语句需要用户具有创建视图的权限
    2. select语句不能包含from子句中的子查询
    3. select语句不能引用系统或用户变量
    4. select 语句不能引用预处理语句参数
    5. 在存储子程序内,定义不能引用子程序参数或局部变量
    6. 在定义中引用的表或视图必须存在
    7. 在定义中不能引用temporary 表,不能创建 temporary 视图
    8. 不能将触发程序与视图关联在一起
    9. 在视图定义中允许使用 order by

    查看视图的定义

         1.  describe view_name  |  desc view_name
         2.  show table status like 'view_name';
         3.  show create view 'view_name';
         4.  select * from database_name.views
         where table_name = 'view_name';
         
    

    修改视图定义

    • create or replace view
    create or replace view 
       [algorithm = {undefined | merge | temptable }]
            view view_name [ {  column_list  } ]
                    as select_statement
                         [with [cascaded  | local ] check option ]
    
    • alter view
    alter view 
       [algorithm = {undefined | merge | temptable }]
            view view_name [ {  column_list  } ]
                    as select_statement
                         [with [cascaded  | local ] check option ]
    
    • 删除视图
        drop view [if exists ]  view_name [, view_name2]..
        restrict  | cascade ]
    
    • 视图数据更新
  • 相关阅读:
    简述Mesos API–files
    docker-compose常用命令
    Linux命令行--使用linux环境变量(转)
    docker:从 tomcat 容器连接到 mysql 容器
    开发环境、生产环境、测试环境的基本理解和区别(转)
    Linux命令行–更多bash shell命令(转)
    docker启动Mysql(转)
    Linux命令行–基本的bash shell命令
    浅谈 man 命令的日常使用
    Linux命令行–走进shell
  • 原文地址:https://www.cnblogs.com/duoban/p/11742933.html
Copyright © 2011-2022 走看看