当前位置:网站首页>MySQL database - views and indexes
MySQL database - views and indexes
2022-07-30 19:22:00 【And with heart】
系列文章目录

前言
Learned in the previous sectionsMySQLBasic operating language,This section will learnMySQLthe more important section,视图和索引.
一、视图
视图的作用:
Views are equivalent to aliasing query statements,方便查询.
视图关键字:view;
1.视图的创建
①.创建视图的基本格式
create view <视图名> as <查询语句>; -- 视图的本质就是用<视图名>来代替<查询语句>,It is equivalent to aliasing the query statement.
②.视图的创建举例
如:对以下查询语句创建视图select * from class; create view v_c as select * from class; -- 创建了一个视图,名字叫做V_C,用于代替查询语句“select * from class”
2.视图的使用
①.视图使用的基本格式:
select <属性名> from<视图名>;
如:select * from v_c; -- Query all properties in the view;
②.Attempt to replace complex statements
We will find out when we query the statement,If we query too many properties,If you want to query multiple times,Then you need to write a long code every time,replace with view,The amount of code can be greatly reduced.
例如:select S.s_name 学生姓名,C.c_name 班级名称 ,T.t_name 教师姓名 from student S left join class C on S.s_cid=C.c_id left join teacher T on C.c_tid=T.t_id;
可以replace with view:create view v_strname as select S.s_name 学生姓名,C.c_name 班级名称 ,T.t_name 教师姓名 from student S left join class C on S.s_cid=C.c_id left join teacher T on C.c_tid=T.t_id
查询此视图:select * from;
3.视图的修改
修改视图的基本格式:alter view<视图名> as <查询语句>;
–让视图名代替另一个查询语句;
4.视图的删除
删除视图的基本格式:drop view <视图名>;
二.索引
1.普通索引
①.创建普通索引
创建普通The basic format of the index:create index 索引名 on 表名<字段1,字段2·······>
例如:
create index IND_SAGE on student(s_age);
-- 对student数据表的s_age创建一个普通索引,索引名为IND_SAGE;
select s_age from student;
-- For query properties,创建一个索引,It will greatly improve the efficiency of the query,The more data the more obvious;
②.以修改表的方式添加索引
The basic format of the index:alter table <表名> add index <索引名(属性)>;
例如:alter table student add index S_ID(s_id); -- In the way of modifying the table asstudent表的s_id属性添加索引;
③.查看表中的所有索引
基本格式:show index from <表名>;
例如:show index from student; -- 查询student数据表中的索引
如果我们没有建立索引,Then the result of the query is not indexed,In fact, there may still be an index,Because if the primary key and foreign key are established,The system will automatically create a primary key index.
例如:
⑤.删除索引
基本格式:drop index <索引名> on <表名>;
如:drop index IND_SEX on student;
2.唯一索引
唯一索引关键字:unique index;
对添加了唯一索引的字段进行查询,查询效率会提高.
Unique indexes are related to unique constraints,给某属性添加唯一索引后,Duplicate values of properties are not allowed;
①.创建唯一约束
基本格式:create unique index <索引名> on <表名(属性名)>;
例如:create unique index S_ID on student(s_id);
②.Add a unique index in the form of a modified table
基本格式:alter table <表名> add unique(属性);
例如:alter table student add unique(s_id);-- 给student数据表中的s_idproperty adds a unique index;
3.主键索引/外键索引
Earlier we mentioned that the primary key index and foreign key index will automatically create the primary key index or foreign key index when the primary key or foreign key is established;
Because the primary key and foreign key index will be automatically established,So we don't need to do it artificially,Only need to establish foreign key and primary key.
The establishment of primary and foreign keys has been explained earlier,这里不做过多的介绍.
4.全文索引
Keywords for full-text indexing:fulltext index
When querying data of string or text type with a full-text index added,The query efficiency will be accelerated.
①.创建表的时候创建
基本格式:create fulltext index <索引名> on <表名(属性名)>;
②.Add a full-text index by modifying the table
基本格式:alter table <表名> add fulltext index fullind_<索引名>(属性名);
Full-text indexing is relatively rare,It is limited to adding to string and text type data;
5.空间索引
空间索引是对空间数据类型(点,线,面,立体图形)建立的索引,Because the spatial index is not widely used,一般不会使用,所以不做详细讲解.
6.索引的优缺点
①.优点:使用索引可以大大提高查询的效率;
②.缺点:定义了索引的字段与普通字段相比,索引会占用额外的磁盘空间,Due to differences in the stored data structure,对数据进行增加,删除,修改等操作的时候,The fields of the established index need to be dynamically maintained,所以增加了DBMS的工作负担,并且降低了数据库的可维护性.
边栏推荐
- 【私人系列】日常PHP遇到的各种稀奇古怪的问题
- Witness the magical awakening of the mini world in HUAWEI CLOUD
- NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
- Perfectly Clear QuickDesk & QuickServer图像校正优化工具
- 电脑死机的时候,发生了什么?
- 第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
- kotlin的by lazy
- [hbuilder] cannot run some projects, open the terminal and cannot enter commands
- Scala学习:breakable
- 解决终极bug,项目最终能顺利部署上线。
猜你喜欢

LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集

生物医学论文有何价值 论文中译英怎样翻译效果好

MindSpore:【JupyterLab】按照新手教程训练时报错

After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

golang日志库zerolog使用记录

MindSpore:ImageFolderDataset数据读取问题

Swiper rotates pictures and plays background music

VBA批量将Excel数据导入Access数据库

C# wpf 无边框窗口添加阴影效果

【网站放大镜效果】两种方式实现
随机推荐
Alibaba Cloud Martial Arts Headline Event Sharing
MindSpore:数据处理问题
055 c# print
Perfectly Clear QuickDesk & QuickServer图像校正优化工具
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
Win11如何更改默认下载路径?Win11更改默认下载路径的方法
crontab中写go run不执行的问题
Swiper rotates pictures and plays background music
vxe-table实现复选框鼠标拖动选中
What is the value of biomedical papers? How to translate the papers into Chinese and English?
常见链表题及其 Go 实现
OneFlow源码解析:Op、Kernel与解释器
Google's AlphaFold claims to have predicted almost every protein structure on Earth
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
云数据库和本地数据库有什么区别?
试写C语言三子棋
来了!东方甄选为龙江农产品直播带货
牛客网——华为题库(100~108)
What is a RESTful API?
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)