mysql在建表语句中添加索引
普通索引創(chuàng)建
創(chuàng)建普通索引,即不添加 UNIQUE、FULLTEXT 等任何參數(shù)。
【例】創(chuàng)建表名為 score 的數(shù)據(jù)表,并在該表的 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)
);
此時(shí)在id字段上建立的普通索引名字為id,在id字段建立的,索引方法為BTREE,索引類型為normal
創(chuàng)建唯一索引
創(chuàng)建唯一索引時(shí),使用 UNIQUE 參數(shù)進(jìn)行約束。
【例】創(chuàng)建表名為 address 的數(shù)據(jù)表,并在該表的 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)
);
此時(shí)在id字段上建立的唯一索引,索引名字為address,索引方法BTREE為,索引類型為Unique
創(chuàng)建前綴索引(某字段前*個(gè)字節(jié))
創(chuàng)建單列索引,即在數(shù)據(jù)表的單個(gè)字段上創(chuàng)建索引。創(chuàng)建該類型索引不需要引入約束參數(shù),用戶在建立時(shí)只需要指定單列字段名,即可創(chuàng)建單列索引。
【例】創(chuàng)建名稱為? telephone? 的數(shù)據(jù)表,并指定在? 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))
);
此時(shí)在tel字段上建立的普通索引,索引名字為tel_num,索引方法為BTREE,索引類型為normal,索引取的是tel字段前20為字節(jié)建立索引
前綴索引無法使用覆蓋索引
創(chuàng)建全文索引
全文索引只能作用在 CHAR、VARCHAR、TEXT、類型的字段上。創(chuàng)建全文索引需要使用 FULLTEXT 參數(shù)進(jìn)行約束。
【例】創(chuàng)建表名為 cards 的數(shù)據(jù)表,并在該表的 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)
);
此時(shí)在name字段上建立的全文索引,索引名字為cards_number,索引方法為空(沒有),索引類型為FULL TEXT
創(chuàng)建多列索引
創(chuàng)建多列索引即指定表的多個(gè)字段即可實(shí)現(xiàn)。
【例】創(chuàng)建名稱為 information 的數(shù)據(jù)表,并指定 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)
);
此時(shí)在name,sex字段上建立的普通聯(lián)合索引,索引名字為info,索引方法為BTREE,索引類型為normal
創(chuàng)建空間索引(在MyISAM上)
創(chuàng)建空間索引時(shí),需要設(shè)置 SPATIAL 參數(shù)。同樣,必須說明的是,只有 MyISAM 類型表支持該類型索引。而且,索引字段必須有非空約束。
【例】創(chuàng)建一個(gè)名稱為 list 的數(shù)據(jù)表,并創(chuàng)建一個(gè)名為 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? 字段上已經(jīng)建立名稱為? listinfo 的空間索引,其中? goods? 字段必須不能為空,且數(shù)據(jù)類型是? GEOMETRY,該類型是空間數(shù)據(jù)類型。空間類型不能用其他類型代替,否則在生成空間素引時(shí)會產(chǎn)生錯誤且不能正常創(chuàng)建該類型索引。
空間類型除了上述示例中提到的 GEOMETRY 類型外,還包括如? POINT、LINESTRING、POLYGON? 等類型,這些空間教據(jù)類型在平常的操作中很少被用到。
參考
原文鏈接:https://blog.csdn.net/qq_41573234/article/details/80250279
總結(jié)
以上是生活随笔為你收集整理的mysql在建表语句中添加索引的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 泛型和集合_Java集合和泛型
- 下一篇: java 日历类_JAVA 的日历类型