当前位置:网站首页>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的工作负担,并且降低了数据库的可维护性.
边栏推荐
- 云数据库和本地数据库有什么区别?
- 开心的聚餐
- Basic use of scrapy
- 监听开机广播
- The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
- C# wpf 无边框窗口添加阴影效果
- Difference between Object and Map
- VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
- 延时队列优化 (2)
- 【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
猜你喜欢

阿里云武林头条活动分享

SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)

iPhone真是十三香?两代产品完全对比,或许上一代更值得买

已删除

nlohmann json 使用指南【visual studio 2022】
![[Summary] 1396- 60+ VSCode plugins to create a useful editor](/img/e4/65e55d0e4948c011585b72733d4d19.jpg)
[Summary] 1396- 60+ VSCode plugins to create a useful editor

redis

OneFlow源码解析:Op、Kernel与解释器

Node encapsulates a console progress bar plugin

云数据库和本地数据库有什么区别?
随机推荐
Start foreground Activity
Chapter 14 Type Information
Correct pose of Vulkan open feature
第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
【PyTorchVideo教程01】快速实现视频动作识别
【私人系列】日常PHP遇到的各种稀奇古怪的问题
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
不同的路径依赖
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
MindSpore:npu 多卡训练自定义数据集如何给不同npu传递不同数据
MindSpore:ImageFolderDataset数据读取问题
node封装一个控制台进度条插件
Listen to the boot broadcast
MindSpore:【语音识别】DFCNN网络训练loss不收敛
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
nlohmann json 使用指南【visual studio 2022】
C# wpf 无边框窗口添加阴影效果
Spark学习:编译Spark项目时遇到的报错
看完《二舅》,我更内耗了
部分分类网络性能对比