一喂柒、創(chuàng)建普通索引:
create table t1 (id int primary key, name varchar(10), sex enum('f','m','un'), index(name)) engine=myisam character set utf8;
二搬俊、創(chuàng)建唯一索引(同時(shí)自定義索引的名字)
create table t1 (id int primary key, name varchar(10), sex enum('f','m','un'), unique index '索引名稱' (name)) engine=myisam character set utf8;
三倦春、創(chuàng)建單列索引(一和二目前都是創(chuàng)建單列索引)
create table t1(
id int primary key,
name varchar(20),
sex enum('f','m','un'),
index 'name_in' ('name' (10)))
engine=myisam character set utf8;
括號(hào)里的10,表示該索引的長(zhǎng)度
四添寺、創(chuàng)建組合索引
create table t1(
id int primary key,
name varchar(20),
sex enum('f','m','un'),
index 'name_in' (id,name,age(100))
)
engine=myisam character set utf8;
注意:
1胯盯、以最左邊作為參考,就是where中用到id有關(guān)的內(nèi)容计露,都會(huì)使用索引博脑,但若是where的是name和age,就不會(huì)使用到索引
mysql> explain select name,age from t4 where id<3 and age<50\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
partitions: NULL
type: range
possible_keys: mutildx
key: mutildx
key_len: 5
ref: NULL
rows: 2
filtered: 33.33
Extra: Using where; Using index
1 row in set, 1 warning (0.01 sec)
ERROR:
No query specified
mysql> show create table t4\G;
*************************** 1. row ***************************
Table: t4
Create Table: CREATE TABLE `t4` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
KEY `mutildx` (`id`,`name`,`age`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
ERROR:
No query specified
mysql> explain select name,age from t4 where age<50\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
partitions: NULL
type: index
possible_keys: NULL
key: mutildx
key_len: 23
ref: NULL
rows: 5
filtered: 33.33
Extra: Using where; Using index
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
五票罐、創(chuàng)建全文索引
所以語句變成
fulltext index 'name' (info);
六叉趣、如何修改和刪除索引
1、使用alter語句
alter table t1 add index 'nameidx' (name(20));
alter table t1 drop index 'nameidx';
2该押、使用create語句
create index nameidx on t1(name);
使用drop語句
drop index 'nameidx' on t1;