要像使用索引提高数据表的访问速度,首先要创建一个索引。
创建索引的方式有三种
- 创建表的时候创建索引
- 使用
create index语句在已经存在的表上创建索引 - 使用
alter table语句在已经存在的表上创建索引
1. 创建表的时候创建索引
创建普通索引
在 t1 表中 id 字段上建立索引,SQL语句如下:
create table t1(
id int,
name varchar(20),
score float,
index(id)
);
创建唯一性索引
创建一个表名为 t2 的表,在表中的id字段上建立索引名为unique_id的唯一性索引,并且按照升序排列,SQL 语句如下:
create table t2(
id int not null,
name varchar(20) not null,
score float,
unique index unique_id(id asc)
);
创建全文索引
创建一个表名为 t3 的表,在表中的name字段上建立索引名为fulltext_name的全文索引,SQL语句如下:
create table t3(
id int not null,
name varchar(20) not null,
score float,
fulltext index fulltext_name(name)
) engine=MyISAM;
注:目前只有MyISAM存储引擎支持全文索引,对于经常需要索引的字符串、文字数据等信息,可以考虑存储到MyISAM存储引擎的表中。
创建单列索引
创建一个表名为 t4 的表,在表中的name字段上建立索引名为 single_name的单列索引,SQL语句如下:
create table t4(
id int not null,
name varchar(20) not null,
score float,
index single_name(name(20))
);
创建多列索引
创建一个表名为 t5 的表,在表中的id和name字段上建立索引名为multi的多列索引,SQL语句如下:
create table t5(
id int not null,
name varchar(20) not null,
score float,
index multi(id,name(20))
);
创建空间索引
创建一个表名为 t6 的表,在空间类型为grometry的字段上创建空间索引,SQL语句如下:
create table t6(
id int,
space geometry not null,
spatial index sp(space)
)engine=myisam;
注:创建空间索引时,所在字段的值不能为空值,并且表的存储引擎为MyISAM。
2. 使用create index语句在已经存在的表上创建索引
创建表
create table book(
bookid int not null,
bookname varchar(255) not null,
authors varchar(255) not null,
info varchar(255) null,
comment varchar(255) null,
publicyear year not null
);
创建普通索引
在book表中的bookid字段上建立一个名称为index_id的普通索引,SQL语句如下:
create index index_id on book(bookid);
创建唯一性索引
在book表中的bookid字段上建立一个名称为uniqueidx的唯一性索引,SQL语句如下所示:
create unique uniqueidx on book(bookid);
创建单列索引
在book表中的comment字段上建立一个名称为singleidex的单列索引,SQL语句如下所示:
create index singleidx on book(comment);
创建多列索引
在book表中的authors和info字段上建立一个名称为mulitidx的多列索引,SQL语句如下所示:
create index mulitidx on book (authors(20),info(20));
创建全文索引
删除表·book·,重新创建表book,在表中的info字段上创建全文索引。
首先删除表book,SQL语句如下:
drop table book;
然后重新创建表book,SQL语句如下:
create table book(
bookid int not null,
bookname varchar(255) not null,
authors varchar(255) not null,
info varchar(255) null,
comment varchar(255) null,
publicyear year not null
)engine=MyISAM;
使用create index语句在book表的info字段上创建名称为fulltextidx的全文索引,SQL语句如下:
create fulltext index fulltextidx on book(info);
创建空间索引
创建表t7,在表中的g字段上创建名称为spatidx的空间索引。
首先创建数据表t7,SQL语句如下:
create table t7(
g geometry not null
)engine=MyISAM;
使用create index语句在t7表的g字段上创建名称为spatidx的空间索引,SQL语句如下:
create spatial index spatidx on t7(g);
3. 使用alter table语句在已经存在的表上创建索引
创建普通索引
为了使book表不包含任何索引,首先删除book表,SQL语句如下:
drop table book;
然后重新建立book,SQL语句如下:
create table book(
bookid int not null,
bookname varchar(255) not null,
authors varchar(255) not null,
info varchar(255) null,
comment varchar(255) null,
publicyear year not null
);
在表中的bookid字段上创建名称为index_id的普通索引,SQL语句如下:
alter table book add index index_id(bookid);
创建唯一性索引
在book表中的bookid字段上建立一个名称为uniqueidx的唯一性索引,SQL语句如下:
alter table book add unique uniqueidx(bookid);
创建单列索引
在book表中的comment字段上建立一个名称为singleidx的单列索引,SQL语句如下所示:
alter table book add index singleidx(comment(50));
创建多列索引
在book表中的authors和info字段上建立一个名称为multidx的多列索引,SQL语句如下:
alter table book add index multidx(authors(20),info(50));
创建全文索引
删除表book,重新创建表book,在表中的info字段上创建全文索引。
首先删除表book,SQL语句如下:
drop table book;
然后重新创建表book,SQL语句如下:
create table book(
bookid int not null,
bookname varchar(255) not null,
authors varchar(255) not null,
info varchar(255) null,
comment varchar(255) null,
publicyear year not null
)engine=MyISAM;
使用alter table语句在book表的info字段上创建名称为fulltextidx的全文索引,SQL语句如下:
alter table book add fulltext index fulltextidx(info);
创建空间索引
创建表t8,在表中的space字段上创建名称为spatidx的空间索引。首先创建数据表t8,SQL语句如下:
create table t8(
space geometry not null
)engine=MyISAM;
使用alter table语句在book表的space字段上创建名称为spatidx的空间索引,SQL语句如下所示:
alter table t8 add spatial index spatidx(space);