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

边栏推荐
- Matplotlib(二)—— 子图
- 【After a while 6】Machine vision video 【After a while 2 was squeezed out】
- 【数据库和SQL学习笔记】7.SQL中的插入(INSERT)、删除(DELETE)、更新(UPDATE)
- 【数据库和SQL学习笔记】8.SQL中的视图(view)
- 通过Flink-Sql将Kafka数据写入HDFS
- Flutter 3.0升级内容,该如何与小程序结合
- 鼠标放上去变成销售效果
- Tensorflow2 与 Pytorch 在张量Tensor基础操作方面的对比整理汇总
- 【Over 15】A week of learning lstm
- Mysql-连接https域名的Mysql数据源踩的坑
猜你喜欢

【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)

Flink 状态与容错 ( state 和 Fault Tolerance)

flink实例开发-详细使用指南

BFC详解(Block Formmating Context)

flink部署操作-flink standalone集群安装部署

flink中文文档-目录v1.4

SQL (2) - join window function view

Tensorflow2 与 Pytorch 在张量Tensor基础操作方面的对比整理汇总

It turns out that the MAE proposed by He Yuming is still a kind of data enhancement

解决:Unknown column ‘id‘ in ‘where clause‘ 问题
随机推荐
Lecture 5 Using pytorch to implement linear regression
el-pagination分页分页设置
flink项目开发-配置jar依赖,连接器,类库
如何停止flink job
全尺度表示的上下文非局部对齐
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
flink on yarn 集群模式启动报错及解决方案汇总
对数据排序
Pandas(五)—— 分类数据、读取数据库
【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA
【数据库和SQL学习笔记】8.SQL中的视图(view)
The fourth back propagation back propagation
【MySQL】数据库多表链接的查询方式
el-table,el-table-column,selection,获取多选选中的数据
基于Flink CDC实现实时数据采集(三)-Function接口实现
ES6基础语法
Distributed and Clustered
Lecture 4 Backpropagation Essays
[Go through 10] sklearn usage record