建立普通索引,即不添加 UNIQUE、FULLTEXT 等任何參數。.net
【例】建立表名爲 score 的數據表,並在該表的 id 字段上創建索引,SQL 語句以下:code
CREATE table score( id int(11) AUTO_INCREMENT primary key not null, name varchar(50) not null, math int(5) not null, English int (5) not null, Chinese int (5) not null, index(id) );
此時在id字段上創建的普通索引名字爲id,在id字段創建的,索引方法爲BTREE,索引類型爲normal
建立惟一索引時,使用 UNIQUE 參數進行約束。orm
【例】建立表名爲 address 的數據表,並在該表的 id 字段上創建惟一索引,SQL 語句以下:blog
CREATE table address( id int(11) auto_increment primary key not null, name varchar(50), address varchar(200), UNIQUE INDEX address(id ASC) );
此時在id字段上創建的惟一索引,索引名字爲address,索引方法BTREE爲,索引類型爲Unique
【例】建立名稱爲 telephone 的數據表,並指定在 tel 字段上創建名稱爲 tel_num 的單列索引,SQL 語句以下:索引
create table telephone( id int(11) primary key auto_increment not null, name varchar(50) not null, tel varchar(50) not null, index tel_num(tel(20)) );
此時在tel字段上創建的普通索引,索引名字爲tel_num,索引方法爲BTREE,索引類型爲normal,索引取的是tel字段前20爲字節創建索引
前綴索引沒法使用覆蓋索引
全文索引只能做用在 CHAR、VARCHAR、TEXT、類型的字段上。建立全文索引須要使用 FULLTEXT 參數進行約束。rem
【例】建立表名爲 cards 的數據表,並在該表的 name 字段上創建全文索引,SQL 語句以下:get
create table cards( id int(11) auto_increment primary key not null, name varchar(50), number bigint(11), info varchar(50), FULLTEXT KEY cards_number(name) );
此時在name字段上創建的全文索引,索引名字爲cards_number,索引方法爲空(沒有),索引類型爲FULL TEXT
建立多列索引即指定表的多個字段便可實現。io
【例】建立名稱爲 information 的數據表,並指定 name 和 sex 爲 多列索引,SQL 語句以下:table
create table information( inf_id int(11) auto_increment primary key not null, name varchar(50) not null, sex varchar(5) not null, birthday varchar(50) not null, index info(name,sex) );
此時在name,sex字段上創建的普通聯合索引,索引名字爲info,索引方法爲BTREE,索引類型爲normal
建立空間索引時,須要設置 SPATIAL 參數。一樣,必須說明的是,只有 MyISAM 類型表支持該類型索引。並且,索引字段必須有非空約束。form
【例】建立一個名稱爲 list 的數據表,並建立一個名爲 listinfo 的空間索引,SQL語句以下:
create table list( id int(11) primary key auto_increment not null, goods geometry not null, SPATIAL INDEX listinfo(goods) )engine=MyISAM;
goods 字段上已經創建名稱爲 listinfo 的空間索引,其中 goods 字段必須不能爲空,且數據類型是 GEOMETRY,該類型是空間數據類型。空間類型不能用其餘類型代替,不然在生成空間素引時會產生錯誤且不能正常建立該類型索引。
空間類型除了上述示例中提到的 GEOMETRY 類型外,還包括如 POINT、LINESTRING、POLYGON 等類型,這些空間教據類型在日常的操做中不多被用到。