zoukankan      html  css  js  c++  java
  • 第4章 mysql中的索引

           MySQL中的索引相当于在word文档中的目录,能够更快的查找出想要的内容,但是索引不是越多越好,就像文档的目录,如果多了会占用很多的纸张,同样MySQL的索引多了也会占系统的空间,并且更新数据库的时候还要维护索引,索引多了维护量就大,现在我们来具体看一下什么是索引吧。

    1.简介

       索引,在关系行数据库中是对表中一列或者多列的值进行排列的一种存储结构。我们要在更新频繁,读取较少的数据少建立索引。那到底在哪创建索引才是合适的呢?

    2.如何创建索引

      比如;select user ,host from mysql.user where host=.....

      索引一定要创建在where后条件列,而不是SELECT选择数据列,另外我们要尽量选择在唯一值较多的大表上建立索引。

    3. 索引分类

      一般索引分三类,普通索引,唯一索引和主键索引。

    • 普通索引:是最基础的索引之一,没有限制条件
    • 唯一索引:要求字段值都是唯一的,而且允许有空值
    • 主键索引:不仅要求字段值都是唯一的,而且不能有空值
    • 全文索引:文件类型或者长字符串上设置的主要用于做文章内容的索引

    4.创建索引的方法

    创建普通索引

    • 使用create index进行创建
    • MySQL [Carrie]> create index index_name on student_data (id);
      Query OK, 0 rows affected (0.12 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
    • 修改表结构时加索引 
    •  使用alter  ....  and进行修改表结构时添加索引
      MySQL [Carrie]> alter table hanjiali add index intdex_name(id);
      Query OK, 0 rows affected (0.01 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
    •  创建表时直接加索引
    •  使用create 创建表时加入索引
      MySQL [Carrie]> create table Carrie(id int not null,username varchar(10),index index_name (username(10)));
      Query OK, 0 rows affected (0.01 sec)
      
      MySQL [Carrie]> show tables;
      +------------------+
      | Tables_in_Carrie |
      +------------------+
      | Carrie           |
      | hanjiali         |
      | student_data     |
      +------------------+
      3 rows in set (0.00 sec)
      
    •  删除索引
    •  直接用drop ...on进行删除
      MySQL [Carrie]> drop index index_name on Carrie;
      Query OK, 0 rows affected (0.01 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
    • 用alter ... drop 也可以删除索引
      MySQL [Carrie]> alter table hanjiali drop index index_name;
      Query OK, 0 rows affected (0.00 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      

        

    创建唯一索引

    •  创建唯一索引  
    •  unique关键词,唯一的
      MySQL [Carrie]> create unique index index_name on hanjiali(name);
      Query OK, 0 rows affected (0.01 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
    • 修改表结构时创建索引
    •  add unique
      MySQL [Carrie]> alter table hanjiali add unique index_name (name);
      Query OK, 0 rows affected (0.00 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
    •  创建表时直接指定索引
    • MySQL [Carrie]> create table mytable(id int not null,username varchar(10),unique index_name (username(10)));
      Query OK, 0 rows affected (0.00 sec)

    创建主键索引 

    •        主键的关键词是primary
    • MySQL [Carrie]> alter table hanjiali  add primary key (name);
      Query OK, 1 row affected (0.04 sec)
      Records: 1  Duplicates: 0  Warnings: 0  

     显示索引信息

    •   
      ySQL [Carrie]> show index from hanjiali;G
      +----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      | Table    | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
      +----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      | hanjiali |          0 | PRIMARY     |            1 | name        | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
      | hanjiali |          0 | index_name  |            1 | name        | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
      | hanjiali |          0 | ind_name    |            1 | name        | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
      | hanjiali |          1 | intdex_name |            1 | id          | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |               |
      +----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      4 rows in set (0.00 sec)
      

        

  • 相关阅读:
    第一课时之导读
    python学习之第十六课时--缩进(indentation)
    python学习之第十六课时--函数的作用及定义
    python学习之第十五课时--存址方式及拷贝
    Linux学习之第七课时--链接(link)文件
    Linux学习之第六课时--文件和目录操作管理命令
    Linux学习之第五课时--文本编辑器
    TOJ--1162---dfs(回溯)
    TOJ---3128---bfs(打印路径)
    TOJ---1502---map真强大
  • 原文地址:https://www.cnblogs.com/hanjiali/p/13982843.html
Copyright © 2011-2022 走看看