回顾:
索引
普通索引:索引字段可重复,可为空
唯一索引:不可重复,可为空
主键索引:不可重复,不可为空
1、组合索引
多个字段索引,只有左边字段被使用时,索引才会被使用。
格式:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
...
字段名n 字段类型n,
index 索引名(字段名1(长度),字段名2(长度),...字段名n(长度))
);
sid,sname,age 只有查询sid,sname,age/ sid,sname/sid,age/sid才会触发组合索引。
第一步:创建组合索引,名字为index_sid_sname_age
create table stu1(
sid int,
sname varchar(20),
age int,
sex varchar(2),
index index_sid_sname_age(sid,sname,age)
);
第二步:什么情况下才会使用组合索引
保证最左前缀,保证查询里面有sid这个字段。
explain select * from stu1 where age=1 and sname='job'; #不能调用组合索引
explain select * from stu1 where sid=1 and sname='job'; #能调用组合索引
2、全文索引
引擎:MyISAM 字段类型:char varchar text 使用函数:match()
格式:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
...
字段名n 字段类型n,
fulltext index 索引名(字段名(长度))
)ENGINE=MyISAM;
3、空间索引
用空间数据类型(点、线、面、几何类型)来建立空间索引。
引擎:MyISAM 列不能为空
格式:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
...
字段名n 字段类型n,
spatial index 索引名(字段名(长度))
)ENGINE = MyISAM;
4、索引操作
创建索引:创建好表后增加索引。
格式:
alter table 数据表名 add [unique|fulltext|spatial] [index|key] [索引名][字段名][asc|desc]
格式:
show index from 数据库表名;
格式一:
alter table 数据库表名 drop index 索引名;
格式二:
drop index 索引名 on 数据库表名;
函数
1、数学函数
abs() abs(-1) 1 abs(1) 1
mod()/% mod(5,2) 1 5%2 1
ceiling() ceiling(1.34) 2. ceil(1.34) 2
round() round(3.58) 4 round(3.456,2) 3.46 round(1234.45,-2) 1200
pi() 3.141593
sqrt() sqrt(9) 3
floor() floor(1.56) 1
sign() sign(100) 1 sign(-100) -1 sign(0) 0
pow() pow(2,3) 8
rand()
truncate()
2、字符串函数
ascii() ascii(‘a’) 97
concat()
length()
locate()
instr()
left()
right()
substring()
trim()
ltrim()
rtrim()
replace()