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

边栏推荐
猜你喜欢

Thread handler handle IntentServvice handlerThread

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

MaskDistill-不需要标注数据的语义分割

Lecture 5 Using pytorch to implement linear regression

【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)

Pandas(五)—— 分类数据、读取数据库

spingboot 容器项目完成CICD部署

【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询

如何停止flink job

【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
随机推荐
Mesos learning
服务网格istio 1.12.x安装
Map、WeakMap
flink项目开发-flink的scala shell命令行交互模式开发
【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询
学习总结week2_4
el-pagination分页分页设置
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
[Go through 9] Convolution
【22李宏毅机器学习】课程大纲概述
盘点关于发顶会顶刊论文,你需要知道写作上的这些事情!
Mysql-连接https域名的Mysql数据源踩的坑
门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作
拿出接口数组对象中的所有name值,取出同一个值
神经网络也能像人类利用外围视觉一样观察图像
发顶会顶刊论文,你应该这样写作
11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!