当前位置:网站首页>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的工作负担,并且降低了数据库的可维护性.
边栏推荐
- The problem of writing go run in crontab does not execute
- WEBSOCKETPP使用简介+demo
- Google's AlphaFold claims to have predicted almost every protein structure on Earth
- What is the value of biomedical papers? How to translate the papers into Chinese and English?
- Zabbix 5.0 监控教程(一)
- [Summary] 1396- 60+ VSCode plugins to create a useful editor
- The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
- vxe-table实现复选框鼠标拖动选中
- How architects grow
- Win11如何更改默认下载路径?Win11更改默认下载路径的方法
猜你喜欢
防抖和节流有什么区别,分别用于什么场景?
LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
开心的聚餐
【PyTorchVideo教程01】快速实现视频动作识别
【hbuilder】运行不了部分项目 , 打开终端 无法输入指令
[Summary] 1396- 60+ VSCode plugins to create a useful editor
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
VBA batch import Excel data into Access database
What is the value of biomedical papers? How to translate the papers into Chinese and English?
VS Code 连接SQL Server
随机推荐
技术很牛逼,还需要“向上管理”吗?
深入浅出边缘云 | 3. 资源配置
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
【PyTorchVideo教程01】快速实现视频动作识别
Scala学习:类和对象
高并发秒杀项目总结
【Prometheus】Prometheus联邦的一次优化记录[续]
nlohmann json 使用指南【visual studio 2022】
JS提升:Promise中reject与then之间的关系
How do radio waves transmit information?
MindSpore:自定义dataset的tensor问题
Swiper轮播图片并播放背景音乐
redis
Start foreground Activity
MindSpore:Cifar10Dataset‘s num_workers=8, this value is not within the required range of [1, cpu_thr
ResNet18-实现图像分类
解决终极bug,项目最终能顺利部署上线。
Swiper rotates pictures and plays background music
AWS console