创建索引:自动创建,创建带主键和唯一约束的表
create table student1(
stu_id int(10) primary key,
stu_name varchar(3) unique,
stu_sex varchar(1)
);
查看该表的索引
show index from 表名;
创建索引:手动创建普通索引
create table student2(
stu_id int(10),
stu_name varchar(3),
index(stu_id)
);
创建索引:手动创建唯一索引
create table student3(
stu_id int(10),
stu_name varchar(3),
unique index(stu_id)
);
创建索引:手动创建主键索引
create table student4(
stu_id int(10),
stu_name varchar(3),
primary key using hash(stu_id)
);
创建索引:手动创建全文索引
create table student5(
stu_id int(10),
stu_info varchar(100),
fulltext index(stu_info)
);
手动创建空间索引
create table student6(
stu_id int(10),
stu_loc point not null,
spatial index(stu_loc)
);
手动创建复合索引
create table student7(
stu_id int(10),
stu_name varchar(3),
index(stu_id,stu_name)
);
创建索引:为已存在的表创建索引
create [unique|fulltext|spatial] index 索引名 [索引类型] on 表名(索引字段名1 [(索引长度)] [asc|desc], 索引字段名2 [(索引长度)] [asc|desc], ···);
create table student8(
stu_id int(10),
stu_name varchar(3)
);
create index index_id on student8(stu_id);
创建索引:为已存在的表创建索引alter table
alter table student13 add index index_id(stu_id);
alter table student13 add unique index index_id(stu_id);
alter table student13 add primary key index_id(stu_id);
alter table student13 add fulltext index index_info(stu_info);
alter table student13 add spatial index index_loc(stu_loc);
alter table student13 add index index_fh(stu_id,stu_name);
删除索引
alter table 表名 drop index|key 索引名;
使用alter table语句并不能删除主键索引,如果要删主键索引,需要使用
alter table 表名 drop primary key;
使用drop inde)x语句删除
drop index 索引名 on 表名;