mysql上亿数据秒级查询 mysql查询最后一条数据的id( 三 )


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后,通过具体案例演示如何使用create index语句在已经存在的数据表中创建索引,具体如下:
1) 创建普通索引
在book表中的bookid字段上建立一个名称为index_id的普通索引,SQL语句如下:
create index index_id on book (bookid);
这样,即可在book表中,为bookid字段建立一个名称为index_id的普通索引 。
2) 创建唯一性索引
在book表中的bookid字段上建立一个名称为uniqueidx的唯一性索引,SQL语句如下:
create unique index uniqueidx on book (bookid);
这样,即可在book表中,为bookid字段建立一个名称为uniqueidx的唯一性索引 。
3) 创建单列索引
在book表中的comment字段上建立一个名称为singleidx的单列索引,SQL语句如下所示:
create index singleidx on book (comment);
这样,即可在book表中,为comment字段建立一个名称为singleidx的单列索引 。
4) 创建多列索引
在book表中的authors字段和info字段上建立一个名称为mulitidx的多列索引,SQL语句如下所示:
create index mulitidx on book (authors(20), info(20));
这样,即可在book表中,为authors和info字段建立一个名称为mulitidx的多列索引 。
5) 创建全文索引
删除表book,重新创建表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);
这样,即可在book表中,为info字段建立一个名称为fulltextidx的全文索引 。
6) 创建空间索引
创建表t7,在表中的g字段上创建名称为spatialidx的空间索引 。
首先创建数据表t7,SQL语句如下所示:
create table t7 (g geometry not null) engine=MyISAM;
使用create index 语句在t7表的g字段上,创建名称为spatialidx的空间索引,SQL语句如下所示:
create spatial index spatialidx on t7 (g);
这样,即可在t7表中,为g字段建立一个名称为spatialidx的空间索引 。
修改表追加索引在一张已经存在的数据库表中创建索引,除了可以使用create index语句外,还可以使用alter table语句来完成 。其语法格式:
alter table 表名 add [unique|fulltext|spatial] index
索引名 (字段名 [(长度)] [asc|desc])
在上述语法格式中,unique、fulltext和spatial都是可选参数,分别用于表示唯一性索引、全文索引和空间索引;add表示向表中添加字段 。
接下来,同样以book表为例,对不同类型的索引进行说明,为了使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
);
创建好数据表book后,就可以使用alter table语句在已存在的数据表中创建索引了,具体如下:
1) 创建普通索引
在表中的bookid字段上创建名称为index_id的普通索引,SQL语句如下:


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: