当前位置:网站首页>[Database and SQL study notes] 8. Views in SQL
[Database and SQL study notes] 8. Views in SQL
2022-08-05 05:38:00 【takedachia】
Tools: SQL Server 2019 Express
OS: Windows 10
Article table of contents
Database backup used:teaching.bak
Review the table structure:
t_student (S#, Sname, Sex, Age, Major)
t_teacher (T#, Tname, Age, Title)
t_course (C#, Cname, T#)
t_student_course (S#, C#, Score)
view
In our daily work, when we present database data to users, we need to simplify the user's data point of view.
We can define the data scattered in multiple tables together through the view View, so that the user does not need to enter some complex query statements, but only needs to do a simple query against the view.
This can better adapt to the needs of different users for data; at the same time, it defines the scope of data access for users, which is beneficial to the confidentiality of data.
A view is a virtual table whose contents are defined by a query.Like a real table, a view contains a series of named columns and rows of data.
However, views do not exist in the database as stored sets of data values.Row and column data comes from tables referenced by the query that defines the view, and is dynamically generated when the view is referenced.
create view
Create a view:
create view student_maleas select Sname, Cname, Scorefrom t_student, t_student_course, t_coursewhere t_student.S#=t_student_course.S# and t_student_course.C#=t_course.C#and Sex='male'After execution, you can open the view under the database in the object explorer, you can find the view we created: 
We can specify the displayed column names:
create view student_male_detail(Name, Course, Score)as select Sname, Cname, Scorefrom t_student, t_student_course, t_coursewhere t_student.S#=t_student_course.S# and t_student_course.C#=t_course.C#and Sex='male'After creation, we select the view, right-click and select "Select Top 1000 Rows" to see the information.
Alter view
For example, add a new column:
alter view student_male_detailas select t_student.S#, Sname, Cname, Scorefrom t_student, t_student_course, t_coursewhere t_student.S#=t_student_course.S# and t_student_course.C#=t_course.C#and Sex='male'Effect:
drop view
Delete the student_male_detail view
drop view student_male_detailEffect:
How to use views
After creating a view, it can be used directly as a table.
The from clause can be used directly.
Example 1: Query the average score of all boys:
select avg(Score)from student_maleEffect:
If we don't use views, the query becomes:
select avg(Score)from t_student, t_student_coursewhere t_student.S#=t_student_course.S# and Sex='male'So using views can simplify queries.
Example 2: Query the average test scores of each boy:

边栏推荐
- day12函数进阶作业
- 【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作
- vscode要安装的插件
- 学习总结week2_4
- Redux
- Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package
- Kubernetes常备技能
- MaskDistill-不需要标注数据的语义分割
- 【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
- flink on yarn 集群模式启动报错及解决方案汇总
猜你喜欢

记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!

CAP+BASE

MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA

【论文精读】R-CNN 之预测框回归(Bounding box regression)问题详述

Tensorflow2 与 Pytorch 在张量Tensor基础操作方面的对比整理汇总
![[After a 12] No record for a whole week](/img/05/df9aeb04274e308e1341020f836821.jpg)
[After a 12] No record for a whole week

【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)

华科提出首个用于伪装实例分割的一阶段框架OSFormer

Thread handler句柄 IntentServvice handlerThread

轻松接入Azure AD+Oauth2 实现 SSO
随机推荐
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
2022年中总结关键词:裁员、年终奖、晋升、涨薪、疫情
redis cache clearing strategy
【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询
Flink Broadcast 广播变量
el-table,el-table-column,selection,获取多选选中的数据
如何跟踪网络路由链路&检测网络健康状况
[Let's pass 14] A day in the study room
MaskDistill-不需要标注数据的语义分割
实现跨域的几种方式
el-table鼠标移入表格改变显示背景色
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
Matplotlib(一)—— 基础
BFC(Block Formatting Context)
MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA
day8字典作业
基于Flink CDC实现实时数据采集(三)-Function接口实现
浅谈Servlet生命周期
基于Flink CDC实现实时数据采集(二)-Source接口实现
全尺度表示的上下文非局部对齐