当前位置:网站首页>MySQL - 多列索引
MySQL - 多列索引
2022-07-24 10:25:00 【骆驼胡杨】
多列索引
多列索引,是指在创建索引时所关联的字段不是一个字段,而是多个字段,虽然可以通过所关联的字段进行查询,但是只有查询条件中使用了所关联字段中的第一个字段,多列索引才会被使用。
1. 创建表时定义索引
CREATE TABLE tablename(
propname1 type1,
……
propnamen type..n,
INDEX | KEY [indexname] (propname1 [(length)] [ ASC | DESC ],
Propname2 [(length)] [ ASC | DESC ],
... ...
Propnamen [(length)] [ ASC | DESC ])
);
注意:和普通索引定义基本相同,不同之处就是增加了多个索引列。
mysql> create table class(id int, name varchar(64), INDEX index_mult_columns (id, name)); //创建class表, 指定id/name为多列索引
mysql> insert into class values(1, '张三'); //插入数据1
mysql> insert into class values(2, '李四'); //插入数据2
mysql> select * from class where id>0 and name='李四';//查询多列
mysql> select * from class where id>0; //查询单列
mysql> select * from class where name='李四'; //查询单列

2. 已存在的表上创建索引
方法一:
CREATE INDEX indexname ON tablename(
propname1 [(length)] [ ASC | DESC ],
Propname2 [(length)] [ ASC | DESC ],
... ...
Propnamen [(length)] [ ASC | DESC ]
);
在上述语句中,关键字CREATE INDEX表示用来创建多列索引。
如下例表已存在,可通过CREATE语句创建多列索引:
mysql> create table class(id int, name varchar(64)); //创建class表
mysql> create INDEX index_mult_columns ON class(id, name); //追加多列索引
mysql> insert into class values(1, '张三'); //插入数据1
mysql> insert into class values(2, '李四'); //插入数据2
mysql> select * from class where id>0 and name='李四';//查询多列
mysql> select * from class where id>0; //查询单列
mysql> select * from class where name='李四'; //查询单列
方法二:
除了上述两种方式来创建全文索引外,在MySQL中创建全文索引还可以通过SQL语句ALTER来实现,其语法形式如下:
ALTER TABLE tablename ADD INDEX|KEY indexname(
propname1 [(length)] [ ASC | DESC ],
Propname2 [(length)] [ ASC | DESC ],
... ...
Propnamen [(length)] [ ASC | DESC ] );
使用方法
mysql> create table class(id int, name varchar(64)); //创建class表
mysql> alter table class add INDEX index_mult_columns (id, name); //追加多列索引
mysql> insert into class values(1, '张三'); //插入数据1
mysql> insert into class values(2, '李四'); //插入数据2
mysql> select * from class where id>0 and name='李四';//查询多列
mysql> select * from class where id>0; //查询单列
mysql> select * from class where name='李四'; //查询单列
边栏推荐
- js函数调用下载文件链接
- 脚手架内各文件配置说明、组件化开发步骤
- Home raiding II (leetcode-213)
- Simply use golang SQLC to generate MySQL query code
- Golang migrate is easy to use
- OpenGL drawing simple triangles
- WEB安全基础 - - -文件上传(文件上传绕过)
- What does resource pooling and resource pooling mean?
- New:Bryntum Grid 5.1.0 Crack
- Mina framework introduction "suggestions collection"
猜你喜欢

常量指针、指针常量

Figure model 2-2022-5-13

图模型2--2022-5-13

Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?

Jenkins deploys the project and prompts that the module package defined by him cannot be found

【二叉树先导】树的概念和表示方法

MySQL 数据库 JDBC编程
![[STM32 learning] (10) stm32f1 general timer realizes pulse counter](/img/42/1f5104f923b1868dda23bcc425fc62.png)
[STM32 learning] (10) stm32f1 general timer realizes pulse counter

Simply use golang SQLC to generate MySQL query code
![[electronic device note 4] inductance parameters and type selection](/img/b1/6c5cb27903bc1093bf49e278e31353.png)
[electronic device note 4] inductance parameters and type selection
随机推荐
String__
[electronic device note 4] inductance parameters and type selection
Common Unicode encoding range
Interpretation of websocket protocol -rfc6455
Home raiding II (leetcode-213)
Web Security Foundation - file upload (file upload bypass)
Mysql InnoDB下联合索引的索引数量?
Analysis of distributed lock redistribution principle
2022, enterprise unified process platform design and integration specifications refer to thubierv0.1
聚集日志服务器
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality
The concept and representation of a tree
Domain Driven practice summary (basic theory summary and analysis + Architecture Analysis and code design + specific application design analysis V) [easy to understand]
Uniapp calendar component
【剑指 Offer II 115. 重建序列】
fatal: unable to commit credential store: Device or resource busy
Sub query of multi table query_ Single row and single column
Aggregate log server
CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability
Configuration description and componentization development steps of various documents in the scaffold