MySQL的索引以及引擎的展示:
在本篇博客中,主要详细介绍MySQL中的索引,包括单列索引,唯一索引,主键索引,组合索引,全文索引,空间索引等以及对引擎的展示。
单列索引:
`单列索引没有啥特殊的地方,纯粹是为了查数据快一点。有三种创建索引的方法。
create database mydb10;
use mydb10;
-- 方式一
create table student(
sid int primary key,
card_id varchar(20),
name varchar(20),
gender varchar(20),
age int,
birth date,
phone_num varchar(20),
score double,
index index_name(name)
);
-- 方式二 直接创建
create index index_gender on student(gender);
-- 方式三 通过修改表结构来添加索引
alter table student add index index_age(age);
index,是添加索引的关键字。
唯一索引:
唯一索引的牵引值必须是唯一的,不能重复。
下面看创建方法。
create table student2(
sid int primary key,
card_id varchar(20),
name varchar(20),
gender varchar(20),
age int,
birth date,
phone_num varchar(20),
score double,
unique index_name(card_id)
);
-- 方式二 直接创建
create unique index index_gender on student(card_id);
-- 方式三 通过修改表结构来添加索引
alter table student add unique index_age(card_id);
关键字是unique index·。
主键索引:
MySQL会自动在主键列创建一个索引。
组合索引:
组合索引是索引1的组合使用
use mydb10;
create index index_phone_name on student(phone_num,name); (普通索引)
-- 创建唯一索引
create unique index index_phone_name on student(phone_num,name);
全文索引:
关于全文索引,用的不多,了解就好。
use mydb10;
create table t_article (
id int primary key auto_increment ,
title varchar(255) ,
content varchar(1000) ,
writing_date date -- ,
-- fulltext (content) -- 创建全文检索
);
insert into t_article values(null,"Yesterday Once More","When I was young I listen to the radio",'2021-10-01');
insert into t_article values(null,"Right Here Waiting","Oceans apart, day after day,and I slowly go insane",'2021-10-02');
insert into t_article values(null,"My Heart Will Go On","every night in my dreams,i see you, i feel you",'2021-10-03');
insert into t_article values(null,"Everything I Do","eLook into my eyes,You will see what you mean to me",'2021-10-04');
insert into t_article values(null,"Called To Say I Love You","say love you no new year's day, to celebrate",'2021-10-05');
insert into t_article values(null,"Nothing's Gonna Change My Love For You","if i had to live my life without you near me",'2021-10-06');
insert into t_article values(null,"Everybody","We're gonna bring the flavor show U how.",'2021-10-07');
-- 修改表结构添加全文索引
alter table t_article add fulltext index_content(content)
-- 添加全文索引
create fulltext index index_content on t_article(content);
-- 使用全文索引
select * from t_article where match(content) against('yo'); -- 没有结果
select * from t_article where match(content) against('you'); -- 有结果
select * from t_article where content like '%you%';
空间索引:
Geometry 空间数据
Point 点
lineString 线
Polygon 多边形
引擎:
show engines;
create database mydb11_engines;
use mydb11_engines;
create table stu1(id int, name varchar(20));
show create table stu1;
-- 创建表指定存储引擎
create table stu2(id int, name varchar(20)) engine=MYISAM;
show create table stu2;