当前位置:网站首页>[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_detail
Effect:
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_male
Effect:
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:
边栏推荐
猜你喜欢
解决端口占用问题
flink实例开发-batch批处理实例
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
Machine Learning (1) - Machine Learning Fundamentals
大型Web网站高并发架构方案
SQL(1) - Add, delete, modify and search
flink部署操作-flink standalone集群安装部署
【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
【Kaggle项目实战记录】一个图片分类项目的步骤和思路分享——以树叶分类为例(用Pytorch)
随机推荐
如何编写一个优雅的Shell脚本(一)
spingboot 容器项目完成CICD部署
实现跨域的几种方式
【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)
【数据库和SQL学习笔记】7.SQL中的插入(INSERT)、删除(DELETE)、更新(UPDATE)
学习总结day5
el-table,el-table-column,selection,获取多选选中的数据
【论文精读】Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation(R-CNN)
Do you use tomatoes to supervise your peers?Add my study room, come on together
【NFT网站】教你制作开发NFT预售网站官网Mint作品
MySql之索引
CVPR 2022 |节省70%的显存,训练速度提高2倍
CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review
学习总结week2_3
Tensorflow踩坑笔记,记录各种报错和解决方法
Thread handler句柄 IntentServvice handlerThread
Machine Learning (1) - Machine Learning Fundamentals
flink on yarn 集群模式启动报错及解决方案汇总
ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
如何编写一个优雅的Shell脚本(二)