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
);
創(chuàng)建好數(shù)據(jù)表book后,通過具體案例演示如何使用create index語句在已經(jīng)存在的數(shù)據(jù)表中創(chuàng)建索引,具體如下:
1) 創(chuàng)建普通索引
在book表中的bookid字段上建立一個(gè)名稱為index_id的普通索引,SQL語句如下:
create index index_id on book (bookid);
這樣,即可在book表中,為bookid字段建立一個(gè)名稱為index_id的普通索引 。
2) 創(chuàng)建唯一性索引
在book表中的bookid字段上建立一個(gè)名稱為uniqueidx的唯一性索引,SQL語句如下:
create unique index uniqueidx on book (bookid);
這樣,即可在book表中,為bookid字段建立一個(gè)名稱為uniqueidx的唯一性索引 。
3) 創(chuàng)建單列索引
在book表中的comment字段上建立一個(gè)名稱為singleidx的單列索引,SQL語句如下所示:
create index singleidx on book (comment);
這樣,即可在book表中,為comment字段建立一個(gè)名稱為singleidx的單列索引 。
4) 創(chuàng)建多列索引
在book表中的authors字段和info字段上建立一個(gè)名稱為mulitidx的多列索引,SQL語句如下所示:
create index mulitidx on book (authors(20), info(20));
這樣,即可在book表中,為authors和info字段建立一個(gè)名稱為mulitidx的多列索引 。
5) 創(chuàng)建全文索引
刪除表book,重新創(chuàng)建表book,在book表中的info字段上建立全文索引 。首先刪除book表 。SQL語句如下所示:
drop table book;
然后重新創(chuàng)建表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字段上創(chuàng)建名稱為fulltextidx的全文索引,SQL語句如下所示:
create fulltext index fulltextidx on book (info);
這樣,即可在book表中,為info字段建立一個(gè)名稱為fulltextidx的全文索引 。
6) 創(chuàng)建空間索引
創(chuàng)建表t7,在表中的g字段上創(chuàng)建名稱為spatialidx的空間索引 。
首先創(chuàng)建數(shù)據(jù)表t7,SQL語句如下所示:
create table t7 (g geometry not null) engine=MyISAM;
使用create index 語句在t7表的g字段上,創(chuàng)建名稱為spatialidx的空間索引,SQL語句如下所示:
create spatial index spatialidx on t7 (g);
這樣,即可在t7表中,為g字段建立一個(gè)名稱為spatialidx的空間索引 。
修改表追加索引在一張已經(jīng)存在的數(shù)據(jù)庫表中創(chuàng)建索引,除了可以使用create index語句外,還可以使用alter table語句來完成 。其語法格式:
alter table 表名 add [unique|fulltext|spatial] index
索引名 (字段名 [(長度)] [asc|desc])
在上述語法格式中,unique、fulltext和spatial都是可選參數(shù),分別用于表示唯一性索引、全文索引和空間索引;add表示向表中添加字段 。
接下來,同樣以book表為例,對(duì)不同類型的索引進(jìn)行說明,為了使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
);
創(chuàng)建好數(shù)據(jù)表book后,就可以使用alter table語句在已存在的數(shù)據(jù)表中創(chuàng)建索引了,具體如下:
1) 創(chuàng)建普通索引
在表中的bookid字段上創(chuàng)建名稱為index_id的普通索引,SQL語句如下:
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- 托帕石價(jià)格還有上升的空間嗎?什么顏色的托帕石好?
- 2元一個(gè)的茶葉蛋,教你獨(dú)門秘方,簡單易上手,茶香濃郁特別入味
- 春季干燥易上火,記得多吃這蔬菜,應(yīng)季而食營養(yǎng)高,清火去春燥
- 酒店防止攝像頭的辦法
- 電跳閘了開關(guān)推不上去
- 史上最牛爆笑零分作文 氣死99個(gè)老師的作文
- 臉上有痘印毛孔粗大怎么辦
- 每逢佳節(jié)胖三斤?趕緊試試這4款遮肉單品,讓你視覺上秒瘦10斤
- 迅速消除工作疲勞的5個(gè)小動(dòng)作
- 上班族走路減肥有4中方法
