当前位置:网站首页>【数据库和SQL学习笔记】8.SQL中的视图(view)
【数据库和SQL学习笔记】8.SQL中的视图(view)
2022-08-05 05:15:00 【takedachia】
工具:SQL Server 2019 Express
操作系统:Windows 10
用到的数据库备份: teaching.bak
回顾一下表结构:
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)
我们在平时工作中,把数据库的数据呈现给用户,就需要简化用户的数据观点。
我们可以把分散在多张表中的数据,通过视图View定义在一起,这样用户就不必输入一些复杂的查询语句,只需要针对视图做简单查询即可。
这样可以更好地适应不同用户对数据的需求;同时这样为用户划定了访问数据的范围,有利于数据的保密。
视图是是一个虚拟表,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据。
但是,视图并不在数据库中以存储的数据值集形式存在。行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成。
创建视图(create view)
创建视图:
create view student_male
as select Sname, Cname, Score
from t_student, t_student_course, t_course
where t_student.S#=t_student_course.S# and t_student_course.C#=t_course.C#
and Sex='男'
执行后,可在对象资源管理器中打开数据库下的视图,可找到我们创建的视图:
我们可以指定显示的列名:
create view student_male_detail(Name, Course, Score)
as select Sname, Cname, Score
from t_student, t_student_course, t_course
where t_student.S#=t_student_course.S# and t_student_course.C#=t_course.C#
and Sex='男'
创建后,我们选择该视图,右键点选“选择前1000行”就可以看到信息了。
修改视图(alter view)
比如新增一列:
alter view student_male_detail
as select t_student.S#, Sname, Cname, Score
from t_student, t_student_course, t_course
where t_student.S#=t_student_course.S# and t_student_course.C#=t_course.C#
and Sex='男'
效果:
删除视图(drop view)
删除 student_male_detail 视图
drop view student_male_detail
效果:
如何使用视图
创建视图后,就可以直接当作一张表来使用。
可直接使用from子句。
例1:查询所有男生的平均分:
select avg(Score)
from student_male
效果:
如果我们不利用视图,查询就变成:
select avg(Score)
from t_student, t_student_course
where t_student.S#=t_student_course.S# and Sex='男'
所以利用视图可以简化查询。
例2:查询每一名男生的考试成绩平均分:

边栏推荐
- 物理层的接口有哪几个方面的特性?各包含些什么内容?
- The software design experiment four bridge model experiment
- In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
- [Go through 10] sklearn usage record
- Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
- flink部署操作-flink on yarn集群安装部署
- 【Reading】Long-term update
- [Over 17] Pytorch rewrites keras
- 通过Flink-Sql将Kafka数据写入HDFS
- 02.01-----The role of parameter reference "&"
猜你喜欢

【过一下10】sklearn使用记录

轻松接入Azure AD+Oauth2 实现 SSO
![[Over 17] Pytorch rewrites keras](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
[Over 17] Pytorch rewrites keras

【NFT开发】设计师无技术基础保姆级开发NFT教程在Opensea上全套开发一个NFT项目+构建Web3网站

【过一下12】整整一星期没记录

【NFT网站】教你制作开发NFT预售网站官网Mint作品

【过一下3】卷积&图像噪音&边缘&纹理

IDEA 配置连接数据库报错 Server returns invalid timezone. Need to set ‘serverTimezone‘ property.

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

【After a while 6】Machine vision video 【After a while 2 was squeezed out】
随机推荐
对数据排序
Redux
flink项目开发-flink的scala shell命令行交互模式开发
Thread handler句柄 IntentServvice handlerThread
学习总结week3_1函数
es6迭代协议
[Skill] Long-term update
【过一下7】全连接神经网络视频第一节的笔记
In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
[Go through 8] Fully Connected Neural Network Video Notes
软件设计 实验四 桥接模式实验
学习总结week2_4
学习总结week3_4类与对象
将照片形式的纸质公章转化为电子公章(不需要下载ps)
Flink Broadcast 广播变量
BFC详解(Block Formmating Context)
What are the characteristics of the interface of the physical layer?What does each contain?
flink实例开发-batch批处理实例
The role of the range function
通过Flink-Sql将Kafka数据写入HDFS