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

边栏推荐
猜你喜欢
随机推荐
用GAN的方法来进行图片匹配!休斯顿大学提出用于文本图像匹配的对抗表示学习,消除模态差异!
Flutter 3.0升级内容,该如何与小程序结合
day9-字符串作业
day6-列表作业
AIDL详解
vscode要安装的插件
spark-DataFrame数据插入mysql性能优化
Matplotlib(二)—— 子图
基于Flink CDC实现实时数据采集(二)-Source接口实现
Flink 状态与容错 ( state 和 Fault Tolerance)
对数据排序
学习总结week2_5
[Skill] Long-term update
【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
day7-列表作业(1)
如何编写一个优雅的Shell脚本(三)
面向小白的深度学习代码库,一行代码实现30+中attention机制。
发顶会顶刊论文,你应该这样写作
序列基础练习题
flink中文文档-目录v1.4







