zoukankan      html  css  js  c++  java
  • 索引概述

    - **索引定义**

      对数据库表的一列或多列的值进行排序的一种结构(Btree方式)

    - **优点**

      加快数据检索速度

    - **缺点**

      占用物理存储空间(var/lib/mysql)
      当对表中数据更新时,索引需要动态维护,降低数据维护速度

    - **索引示例**

    ```mysql
    1、开启运行时间检测
        mysql>show variables like "%pro%";
        mysql>set profiling=1;
    2、执行查询语句
        select name from students where name = 'Tom99999';
    3、查看执行时间
        show profiles;
    4、在name字段创建索引
        create index name on students(name)
    5、再执行查询语句
        select name from students where name = 'Tom88888';
    6、查看执行时间
        show profiles;
    ```

    ## 索引分类

      普通(MUL)   唯一(UNI)

    - **使用规则**

      1、可设置多个字段
      2、普通索引 :字段值无约束,KEY标志为 MUL
      3、唯一索引(unique) :字段值不允许重复,但可为 NULL , KEY标志为 UNI
      4、那些字段创建索引:经常用来查询的字段、where条件判断字段、order by排序的字段

    - **创建普通索引and唯一索引**

      1.创建表时

      create table 表名(
      字段名 数据类型,
      字段名 数据类型,
      index(字段名),
      index(字段名),
      unique(字段名)
      );


      2.已有表中创建

        create [unique] index 索引名 on 表名(字段名);

    - **查看索引**

       1、desc 表名; --> KEY标志为:MUL 、UNI
      2、show index from 表名G;

    - **删除索引**

      drop index 索引名 on 表名;

    #### **主键(PRI)and自增长(auto_increment)**

      - **使用规则**

      1、只能有一个主键字段
      2、所带约束 :不允许重复,且不能为NULL
      3、KEY标志 :PRI
      4、通常设置记录编号字段id,能唯一锁定一条记录

      - **创建**

        1.创建表添加主键

              create table student(id int primary key auto_increment)auto_increment=10000;##设置自增长起始值

          2.已有表添加主键

         alter table 表名 add primary key(id);

        3.已有表操作自增长属性

         alter table 表名 modify id int auto_increment;

        4.已有表重新指定起始值

         alter table 表名 auto_increment=20000;

      - **删除**

        删除自增长属性(modify)

          alter table 表名 modify id int; 

        删除主键索引

          alter table 表名 drop primary key;(需要先删除自增长属性才能删除主键)    

  • 相关阅读:
    异步任务AsyncTask
    巧用TextView实现分隔线
    android系统的常用权限
    浅析对话框AlertDialog
    LinearLayout中的layout_weight属性详解
    CLOB大数据对象
    模糊查询demo
    ES6 箭头函数
    ES6中数组求和,求平均数方法( reduce )
    ES6中数组方法( every 和 some )
  • 原文地址:https://www.cnblogs.com/Acekr/p/11027875.html
Copyright © 2011-2022 走看看