mysql字段简索引_MySQL中索引使用简例

一张表最多不超过4个索引

某个字段的值离散度越高,该字段越适合做索引的关键字。

占用储存空间少的字段更适合选做索引的关键字。

较频繁的作为where查询条件的字段应该创建索引,分组字段或者排序字段应该创建索引,两个表的连接字段应该创建索引。

更新频繁的字段不适合做索引,不会出现在where中的字段不应该创建索引。

最左前缀原则:

index(id,name,age)

查询id='' name='' age='' 会使用索引

id='' name='' 不会

id='' age='' 不会,必须从左至右

尽量使用前缀索引

全文索引fulltext只有在MySAM引擎中有效

show index from 表名;郑州去胎记医院http://www.zykdbhk.com/

查看表中的索引。

注意:在指定主键的时候,数据库会自动在主键字段上创立主键索引.

建表时创建索引语法:

create table 表名(

字段名1 数据类型 [约束条件],

[其他约束条件],

[ unique | fulltext ] index [索引名] ( 字段

名 [(长度)] [ asc | desc ] )

) engine=存储引擎类型 default charset=

字符集类型.

实例:

create table book(

isbn char(20) primary key,

name char(100) not null,

brief_introduction text not null,

price decimal(6,2),

publish_time date not null,

unique index isbn_unique (isbn),

index name_index (name (20)),

fulltext index brief_fulltext

(name,brief_introduction),

index complex_index (price,publish_time)

) engine=MyISAM default charset=gbk;

123456789101112

在已有表上创建索引:

语法格式⼀:

create [ unique | fulltext ] index 索引名 on 表名 ( 字段

名 [(长度)] [ asc | desc ] )

语法格式⼆:

alter table 表名 add [ unique | fulltext ] index 索引名

( 字段名 [(长度)] [ asc | desc ] )

实例:

create unique index name_index on studentinfo(name(3));

使用索引的格式:

select studentid,score

from score use index(索引名)

where studentid>=3;

select studentid,score

from score use index(index_stuid)

where studentid>=3;

在其他字段上创建的索引,但查询的studentid上没有,在查询的时候调用其他字段的索引

删除索引:

drop index 索引名 on 表名;

视图

create view ppcourse4

as

select

fir.cno,fir.cname as c1,third.cname as c2

from

course fir,course sec,course third

where

fir.cpno = sec.cno

and

sec.cpno = third.cno

select * from ppcourse4

ppcourse4

delete from ppcourse4

drop view ppcourse4

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值