zoukankan      html  css  js  c++  java
  • MySQL索引的操作

    索引的概念:数据库对象索引其实与书的目录类似,

                    主要是为了提高从表中检索数据的速度。

    (2)根据索引的存储类型可以将索引分为B型索引(BTREE)和哈希索引(HASH)。

    (3)MySQL所支持的索引:普通索引、唯一索引、全文索引、单列索引、多列索引和空间索引。

    一、创建和查看普通索引

    (1)创建表时创建普通索引

     create table 课程表(
    
     课程编号 char(3),
    
     课程名称 varchar(20),
    
     课时 float,
    
     专业编号 char(3),
    
     教师编号 char(4),
    
     index index_课程名称(课程名称)
    
     );
    
     show create table 课程表 G
    
     explain select * from 课程表 where 课程名称=1G
    
     drop index index_课程名称 on 课程表;   \删除刚刚创建的索引

    (2)在已经存在的表上创建普通索引

    create index index_课程名称 on 课程表(课程名称);
    
     show create table 课程表 G
    
     drop index index_课程名称 on 课程表;

    (3)通过SQL语句alter table创建普通索引

     alter table 课程表 add index index_课程名称(课程名称);
    
     show create table 课程表 G
    
     drop index index_课程名称 on 课程表;

    二、创建和查看唯一索引

    (1)创建表时创建唯一索引

    create table 班级表(
    
     班级编号 char(3),
    
     专业编号 char(3),
    
     班主任编号 char(4),
    
     unique index index_班级编号(班级编号)
    
     );
    explain select * from 班级表 where 班级编号=1 G
    
     drop index index_班级编号 on 班级表;

    (2)在已经存在的表上创建唯一索引

    create unique index index_班级编号 on 班级表(班级编号);
    
     show create table 班级表 G
    
     drop index index_班级编号 on 班级表;

    (3)通过SQL语句alter table创建唯一索引

     alter table 班级表 add unique index index_班级编号(班级编号);
    
     show create table 班级表 G
    
     drop index index_班级编号 on 班级表;

    三、创建和查看全文索引

    drop table 教师表;

    (1)创建表时创建全文索引

    create table 教师表(
    
     教师编号 varchar(10),
    
     教师姓名 varchar(50),
    
     职务 varchar(6),
    
     性别 char(1),
    
     年龄 int,
    
     fulltext index index_教师姓名(教师姓名)
    
     )engine=MyISAM;
    
     show create table 教师表 G
    
     drop index index_教师姓名 on 教师表;

    (2)在已经存在的表上创建全文索引

    create fulltext index index_教师姓名 on 教师表(教师姓名);
    
     show create table 教师表 G
    
     drop index index_教师姓名 on 教师表;

    (3)通过SQL语句alter table创建全文索引

    alter table 教师表 add fulltext index index_教师姓名(教师姓名);
    
     show create table 教师表 G
    
     drop index index_教师姓名 on 教师表;

    四、创建和查看多列索引

    (1)创建表时创建多列索引

    create table 专业表(
    
     专业编号 char(3),
    
     专业名称 varchar(20),
    
     key index_专业编号_专业名称(专业编号,专业名称)
    
     );
    
     show create table 专业表 G
    
     drop index index_专业编号_专业名称 on 专业表;

    (2)在已经存在的表上创建多列索引

     create index index_专业编号_专业名称 on 专业表(专业编号,专业名称);
    
     show create table 专业表 G
    
     drop index index_专业编号_专业名称 on 专业表;

    (3)通过SQL语句alter table创建多列索引

    alter table  专业表 add index index_专业编号_专业名称(专业编号,专业名称);
    
     show create table 专业表 G
    
     drop index index_专业编号_专业名称 on 专业表;
     
  • 相关阅读:
    Schema和数据类型优化?
    语雀发布博客园
    为知笔记文章目录
    码云搭建博客
    springboot的过滤器、监听器、拦截器
    springboot常用注解
    springboot使用小技巧合集
    springboot整合swagger2
    强制卸载win软件
    xshell下载和优化配置
  • 原文地址:https://www.cnblogs.com/Jackbk/p/12692822.html
Copyright © 2011-2022 走看看