数据库的索引

  作者:记性不好的阁主

表结构:




索引


 分类:

----主键索引: 不能重复。id 不能是null

----唯一索引 :不能重复。id 可以是null

----单值索引 : 单列, age ;一个表可以多个单值索引,name。

----复合索引 :多个列构成的索引 (相当于 二级目录 : z: zhao) (name,age) (a,b,c,d,...,n)


 创建索引:


  方式一:

  create 索引类型 索引名 on 表(字段)


  单值索引:

  create index dept_index on tb(dept);

  唯一索引:

  create unique index name_index on tb(name) ;

  复合索引

  create index dept_name_index on tb(dept,name);


  方式二:

  alter table 表名 索引类型 索引名(字段)


  单值索引:

  alter table tb add index dept_index(dept) ;

  唯一索引:

  alter table tb add unique index name_index(name);

  复合索引

  alter table tb add index dept_name_index(dept,name);


  注意:如果一个字段是primary key,则改字段默认就是 主键索引


  删除索引:

  drop index 索引名 on 表名 ;

  drop index name_index on tb ;


  查询索引:

  show index from 表名 ;



  扩展:格式化查看表信息,查询命令+ \G;


相关推荐

评论 抢沙发

表情

分类选择