MySQL优化系列随笔-2

一、创建一张表

-- 创建表
create table if not exists tb1(
	id int(10) primary key auto_increment comment "主键自增",
    name varchar(22) not null default "小明" comment "用户名",
    age int(3) not null comment "年龄"
)ENGINE=MYISAM DEFAULT CHARSET=UTF8;

二、索引分类

  1. 单值索引 : 单列。一个表可以有多个单值索引。
  2. 唯一索引 :不能重复。可以是null。
  3. 复合索引 :多个列构成的索引。【索引相当于是书的目录,而复合索引则相当于二级目录】

三、创建索引

  1. 为name创建一个单值索引

    方式一:

    create index name_index on tb1(name);
    

    方式二:

    alter table tb1 add index name_index(name);
    
  2. 为age创建一个唯一索引【假设age不重复】

    create或者alter都可以

    alter table tb1 add unique index age_index(age);
    

  3. 为name、age创建复合索引

    create或者alter都可以

    create index name_age_index on tb1(name,age);
    

四、查看、删除索引

  1. 查看索引

    show index from tb1;
    

    注意:主键默认为主键索引,和唯一索引类似,它们的区别是主键索引不能为null,而唯一索引可以为null。

  2. 删除索引

    如:删除age_index索引

    drop index age_index on tb1;
    


索引的分类、创建、删除你学会了吗。

posted @ 2020-09-06 20:10  宇宙砍柴人  阅读(119)  评论(0编辑  收藏  举报