当前位置:网站首页>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的工作负担,并且降低了数据库的可维护性.
边栏推荐
- Tensorflow2.0 confusion matrix does not match printing accuracy
- Go 系统收集
- Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
- MindSpore:【JupyterLab】查看数据时报错
- 【PyTorchVideo教程01】快速实现视频动作识别
- MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
- Another company interview
- 启动前台Activity
- 【Prometheus】Prometheus联邦的一次优化记录[续]
- What is the difference between a cloud database and an on-premises database?
猜你喜欢

牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)

【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。

牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)

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

redis

Scrapy framework is introduced

kotlin by lazy

Win11如何更改默认下载路径?Win11更改默认下载路径的方法

node封装一个控制台进度条插件

Recommendation | People who are kind to you, don't repay them by inviting them to eat
随机推荐
055 c# print
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
MindSpore:对image作normalize的目的是什么?
Tensorflow2.0 混淆矩阵与打印准确率不符
Talking about Contrastive Learning (Contrastive Learning) the first bullet
DM8: Single database and single instance to build a local data guard service
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
跨进程启动后台服务
MindSpore: CV.Rescale(rescale,shift)中参数rescale和shift的含义?
Go 系统收集
natural language processing nltk
Multiple instances of mysql
启动前台Activity
JsonUtil基于字符串操作josn
【Prometheus】Prometheus联邦的一次优化记录[续]
MindSpore:npu 多卡训练自定义数据集如何给不同npu传递不同数据
Recommendation | People who are kind to you, don't repay them by inviting them to eat
电脑死机的时候,发生了什么?
架构师如何成长