概念:
分类:
一、建表时候创建索引
基本语法格式:
create table table_name(
属性名 数据类型[约束条件],
……
[UNIQUE | FULLTEXT | SPATIAL ] INDEX | KEY [别名] (属性名1,…属性名n [(长度)] [ASC | DESC])
);
注:
INDEX | KEY:指定索引,两者选择一个即可。
别名:为索引命名。
属性名:索引对应的字段名称,必须被预先定义。
长度:索引的长度,字符串类型才能使用。
ASC/DESC:前者是升序排列,后者是降序排列。
创建表telephone,以及创建长度为20的单列普通索引tel:
create table telephone(
-> id int(11) primary key,
-> name varchar(50) not null,
-> tel varchar(50) not null,
-> index (tel(20))
-> );
Query OK, 0 rows affected (0.03 sec)
创建表information,以及为name,sex列创建索引info:
mysql> create table information(
-> id int(11) primary key,
-> name varchar(50) not null,
-> sex varchar(5) not null,
-> index info(name,sex)
-> );
Query OK, 0 rows affected (0.03 sec)
创建表address的同时为列id以升序的方式创建唯一索引:
mysql> create table address(
-> id int(11) primary key,
-> address varchar(200),
-> unique index address(id ASC)
-> );
Query OK, 0 rows affected (0.02 sec)
创建表cards时为表cards的info列创建全文索引cards_info:
mysql> create table cards(
-> id int(11) primary key,
-> name varchar(50),
-> info varchar(50),
-> fulltext index cards_info(info)
-> );
Query OK, 0 rows affected (0.05 sec)
创建表list的同时为列goods创建空间索引listinfo,并且指定该表的存储引擎为MYISAM类型:
mysql> create table list(
-> id int(11) primary key,
-> goods geometry not null,
-> spatial index listinfo(goods)
-> )
-> engine=MYISAM;
Query OK, 0 rows affected (0.06 sec)
二、在已有的表里进行索引操作
为name字段创建长度为10的索引index_name:
mysql> create index index_name on worklnfo(name(10));
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
在表workInfo里的type和address列上创建名为index_t的索引:
mysql> alter table worklnfo add index index_t(Type,Address);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
在extra字段上创建名为index_ext的全文索引。
mysql> alter table worklnfo add fulltext index index_ext(extra);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
三、查看表索引
基本的语句格式
SHOW INDEX FROM <表名> [ FROM <数据库名>]
查看表workInfo里的所有索引:
mysql> show index from worklnfo;
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| worklnfo | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| worklnfo | 0 | id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| worklnfo | 1 | index_name | 1 | Name | A | 0 | 10 | NULL | | BTREE | | |
| worklnfo | 1 | index_t | 1 | Type | A | 0 | NULL | NULL | YES | BTREE | | |
| worklnfo | 1 | index_t | 2 | Address | A | 0 | NULL | NULL | YES | BTREE | | |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5 rows in set (0.00 sec)
四、删除索引
语句格式
DROP INDEX index_name ON table_name;
使用 DROP 语句删除workinfo表的惟一性索引id:
mysql> drop index id on worklnfo;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0