当前位置:网站首页>MySQL - 普通索引
MySQL - 普通索引
2022-07-24 10:25:00 【骆驼胡杨】
创建和查看索引
- 创建索引是指在某个表的一列或多列上建立一个索引,以便提高对表的访问速度。
- 创建索引有3种方式,分别是创建表的时候创建索引、在已经存在的表上创建索引和使用ALTER TABLE语句来创建索引。本节将根据具体的索引分类详细的讲解这3种创建方法。
普通索引
所谓普通索引,就是在创建索引时,不附加任何限制条件(唯一、非空等限制)。该类型的索引可以创建在任何数据类型的字段上。
1. 创建表时定义索引
CREATE TABLE tablename( propname1 type1, propname2 type2, …… propnamen type..n, INDEX | KEY [indexname] (propnamen [(length)] [ ASC | DESC ] ) );参数INDEX和KEY是用来指定字段为索引的,两者选择其中之一就可以了,作用是一样的;
参数indexname是索引名字,可省略;
参数propnamen是索引对应的字段的名称,该字段必须为前面定义好的字段;
参数length是可选参数,其指索引的长度,必须是字符串类型才可以使用;
参数ASC和DESC都是可选参数,ASC表示升序排列,DESC表示降序排列,如果不指定,则为升序。例:
mysql> create table class(id int, name varchar(64), teacher varchar(64), INDEX id_index (id DESC)); //创建class表, 指定id列为索引列, 排序方式为降序 mysql> show create table class; //查询表结构 mysql> insert into class values(1, '一班', '张三'); //插入数据1 mysql> insert into class values(2, '二班', '李四'); //插入数据2 mysql> select * from class where id>0; //根据列查询

2. 已存在的表上创建索引
方法一: 执行create 语句CREATE INDEX indexname ON tablename (propname [(length)] [ASC|DESC]);参数INDEX是用来指定字段为索引,此处不能为KEY;
参数indexname是新创建的索引的名字;
参数tablename是指需要创建索引的表的名称,该表必须是已经存在的,如果不存在,需要先创建;
参数propname指定索引对应的字段的名称,该字段必须为前面定义好的字段;
参数length是可选参数,表示索引的长度,必须是字符串类型才可以使用;
参数ASC和DESC都是可选参数,ASC表示升序排列,DESC表示降序排列,默认升序。mysql> create table class(id int, name varchar(64), teacher varchar(64)); //创建class表 mysql> create INDEX id_index ON class(id DESC); //指定class表中id为索引列,降序排列 mysql> show create table class; //查询表结构 mysql> insert into class values(1, '一班', '张三'); //插入数据1 mysql> insert into class values(2, '二班', '李四'); //插入数据2 mysql> select * from class where id>0; //根据列查询方法二: 执行ALTER TABLE 语句
ALTER TABLE tablename ADD INDEX | KEY indexname (propname [(length)] [ASC|DESC]);参数tablename是需要创建索引的表;
关键字IDNEX或KEY用来指定创建普通索引;
参数indexname用来指定所创建的索引名;
参数propname用来指定索引所关联的字段的名称;
参数length用来指定索引的长度;
参数ASC用来指定升序排序;参数DESC用来指定降序排序。mysql> create table class(id int, name varchar(64), teacher varchar(64)); //创建class表 mysql> alter table class INDEX id_index (id DESC); //指定class表中id为索引列,降序排列 mysql> show create table class; //查询表结构 mysql> insert into class values(1, '一班', '张三'); //插入数据1 mysql> insert into class values(2, '二班', '李四'); //插入数据2 mysql> select * from class where id>0; //根据列查询
查看索引执行情况
- EXPLAIN 查询语句
- 输出结果:
key:实际使用的索引。如果为NULL,则没有使用索引
possible_keys:显示可能应用在这张表中的索引,一个或多个。查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用
key_len: 表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度。此值越短越好!
边栏推荐
- 聚集日志服务器
- Erlang学习02
- The best time to buy and sell stocks (leetcode-121)
- Uniapp calendar component
- [STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
- The concept and representation of a tree
- Chapter V Modification implementation (impl) class
- Implementation and traversal of binary tree and binary tree sorting tree
- 脚手架文件目录说明、文件暴露
- 2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
猜你喜欢

高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力

How does ribbon get the default zoneawareloadbalancer?

Image processing: rgb565 to rgb888
![[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus](/img/8f/19b0eb959d2b3f896c8e99f8e673d1.png)
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus

Add a love power logo to your website

Ffmpeg splash screen solution (modify the source code and discard incomplete frames)

2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
![[STM32 learning] (10) stm32f1 general timer realizes pulse counter](/img/42/1f5104f923b1868dda23bcc425fc62.png)
[STM32 learning] (10) stm32f1 general timer realizes pulse counter

Deployment and analysis of coredns
![[correcting Hongming] what? I forgot to take the](/img/41/c8fa6380ab63949ae6d904fdbb005c.png)
[correcting Hongming] what? I forgot to take the "math required course"!
随机推荐
Websocket 协议解读-RFC6455
[STM32 learning] (9) stm32f1 general timer realizes simple breathing lamp
Zoj1137+ operation 1 -- May 28, 2022
2022, enterprise unified process platform design and integration specifications refer to thubierv0.1
Ffmpeg splash screen solution (modify the source code and discard incomplete frames)
第五章 修改实现(IMPL)类
2022: I feel like sleeping for the first time during the day
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire
OSPF includes special area experiments, mGRE construction and re release
Analysis of Kube proxy IPVS mode
Image processing: rgb565 to rgb888
【二叉树先导】树的概念和表示方法
Simply use golang SQLC to generate MySQL query code
NIO知识点
Adobe Substance 3D Designer 2021软件安装包下载及安装教程
图像处理:RGB565转RGB888
Build a live broadcast platform based on webrtc
Record AP and map calculation examples
[correcting Hongming] what? I forgot to take the "math required course"!