当前位置:网站首页>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='李四'; //查询单列
边栏推荐
- Implementation and traversal of binary tree and binary tree sorting tree
- AttributeError: module ‘sipbuild. api‘ has no attribute ‘prepare_ metadata_ for_ build_ wheel‘
- The role of glpushmatrix and glpopmatrix
- Association Rules -- July 10, 2022
- CRC Coding in C language
- Uniapp calendar component
- Erlang学习番外
- Differential restraint system -- 1 and 2 -- May 27, 2022
- Uniapp uses PWA
- Wechat applet
猜你喜欢

Ask you to build a small program server

In the envy of LETV, there is a "meaning crisis" of contemporary workers

Uniapp calendar component

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

Curse of knowledge

图像处理:RGB565转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

Zoj1137+ operation 1 -- May 28, 2022

CRC Coding in C language

Homologous policy solutions
随机推荐
[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus
[STM32 learning] (8) stm32f1 general timer configuration
Review of new services and functions of Amazon cloud technology in June 2022
2022, will lead the implementation of operation and maintenance priority strategy
What did zoneawareloadbalancer of ribbon and its parent class do?
The best time to buy and sell stocks Ⅳ (leetcode-188)
MySQL - lock
Balance between management / business and technology
【剑指 Offer II 115. 重建序列】
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
fatal: unable to commit credential store: Device or resource busy
高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力
脚手架内各文件配置说明、组件化开发步骤
Homologous policy solutions
多表查询之子查询_单行单列情况
zoj-Swordfish-2022-5-6
Erlang studies abroad
【二叉树先导】树的概念和表示方法
SMTP automatic mail sending function code
Google Earth engine - QA in Landsat 5 toa dataset_ Pixel and QA_ Radsat band