当前位置:网站首页>[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:

边栏推荐
- 轻松接入Azure AD+Oauth2 实现 SSO
- [Go through 4] 09-10_Classic network analysis
- 如何停止flink job
- MaskDistill-不需要标注数据的语义分割
- [Remember 1] June 29, 2022 Brother and brother double pain
- es6迭代协议
- [Go through 8] Fully Connected Neural Network Video Notes
- Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package
- In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
- npm搭建本地服务器,直接运行build后的目录
猜你喜欢

【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码
![[Over 17] Pytorch rewrites keras](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
[Over 17] Pytorch rewrites keras

A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.

发顶会顶刊论文,你应该这样写作

CVPR最佳论文得主清华黄高团队提出首篇动态网络综述

AWS 常用服务

关于基于若依框架的路由跳转
![[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]](/img/10/7aa3153e861354178f048fb73076f7.png)
[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]

【论文精读】R-CNN 之预测框回归(Bounding box regression)问题详述
![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)
[Go through 10] sklearn usage record
随机推荐
怎样在Disciples门徒获得收益?
Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package
Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
el-pagination左右箭头替换成文字上一页和下一页
实现跨域的几种方式
MaskDistill-不需要标注数据的语义分割
如何停止flink job
CAP+BASE
MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA
哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型
【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
【论文精读】R-CNN 之预测框回归(Bounding box regression)问题详述
【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)
day9-字符串作业
Machine Learning (1) - Machine Learning Fundamentals
day6-列表作业
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
My 的第一篇博客!!!