普通索引創建
創建普通索引,即不添加 UNIQUE、FULLTEXT 等任何引數,
【例】創建表名為 score 的資料表,并在該表的 id 欄位上建立索引,SQL 陳述句如下:
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 引數進行約束,
【例】創建表名為 address 的資料表,并在該表的 id 欄位上建立唯一索引,SQL 陳述句如下:
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 引數進行約束,
【例】創建表名為 cards 的資料表,并在該表的 name 欄位上建立全文索引,SQL 陳述句如下:
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
創建多列索引
創建多列索引即指定表的多個欄位即可實作,
【例】創建名稱為 information 的資料表,并指定 name 和 sex 為 多列索引,SQL 陳述句如下:
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
創建空間索引(在MyISAM上)
創建空間索引時,需要設定 SPATIAL 引數,同樣,必須說明的是,只有 MyISAM 型別表支持該型別索引,而且,索引欄位必須有非空約束,
【例】創建一個名稱為 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 等型別,這些空間教據型別在平常的操作中很少被用到,
參考
原文鏈接:https://blog.csdn.net/qq_41573234/article/details/80250279
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/82518.html
標籤:MySQL
上一篇:mysql添加索引(建表之后)
下一篇:哪個邏輯能力強來看看唄
