索引

创建索引:自动创建,创建带主键和唯一约束的表

##例如:创建一个新表student1,将表中的stu_id字段设置为主键约束,
##stu_name字段设置为唯一约束。
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

#1、使用alter table语句创建普通索引
alter table student13 add index index_id(stu_id);
#2、使用alter table语句创建唯一索引
alter table student13 add unique index index_id(stu_id);
#3、使用alter table语句创建主键索引
alter table student13 add primary key index_id(stu_id);
#4、使用alter table语句创建全文索引
alter table student13 add fulltext index index_info(stu_info);
#5、使用alter table语句创建空间索引
alter table student13 add spatial index index_loc(stu_loc);
#6、使用alter table语句创建复合索引
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 表名;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值