SQL语言增、删、改、查功能练习
目录
- 一、数据库SQL语言增、删、改、查功能
- 二、视图SQL语言功能
- 三、源程序代码和实验结果
一、数据库SQL语言增、删、改、查功能
1.查询选了1号课且选了2号课的学生的班号、学号
2. 查询选了1号课但不选2号课的学生的班号、学号
3.查询1班平均分在85分以上的同学班号、学号、姓名、性别、系、各科课程号及成绩
4.查询至少选了1班2号同学所选课的所有班号、学号及同学姓名
5. 查询不选1号课的学生班号及学号
6. 查询选2号课的学生名字及相应2号课成绩,按成绩从高到低排序
7. 统计学生选修课程的班号、学号及总学分
8. 统计1班选修3号课的学号及平均分
9. 把个人信息及选课信息插入到Student和SC 表及新增加一门“无机化学”课程信息
10. 删除选修3号课的所有选课信息并显示删除后的结果
11. 把选修1号课的所有男同学年龄增加1岁并显示最终学生Student信息
12. 把每个选课人的学号、班号及平均成绩插入到一个新表中。
二、视图SQL语言功能
1 使用企业管理器创建视图:在ST库中以“student”表为基础,建立信息系学生的视图V_ISStudent
2 使用SQL语句创建视图:
① 建立一个每个学生的学号、班号、姓名、选修的课名及成绩的视图S_C_GRADE;
② 建立信息系建立信息系选修了1号课程且成绩在90分以上的学生的视图V_IS_Score
③ 将各系学生人数,平均年龄定义为视图V_NUM_AVG。
3 查询以上所建的视图结果
4 查询选修了1号课程的信息系学生
5 在信息系学生的视图中找出年龄小于20岁的学生
6 将信息系学生视图V_ISStudent中学号一班2号的学生姓名改为“刘辰”
7 用SQL语句删除视图S_C_GRADE
三、源程序代码和实验结果
E-R图:
1.1查询选了1号课且选了2号课的学生的班号、学号
select distinct SCX.sno,SCX.sclass
from SC SCX
where exists ( select *from SC SCYwhere SCX.sno=SCY.sno and SCX.sclass=SCY.sclass and SCY.cno=1)and exists( select * from SC SCZ where SCX.sno=SCZ.sno and SCX.sclass=SCZ.sclass and SCZ.cno=2);
1.2查询选了1号课但不选2号课的学生的班号、学号
select SCX.sno,SCX.sclass
from SC SCX
where exists ( select *from SC SCYwhere SCX.sno=SCY.sno and SCX.sclass=SCY.sclass and SCY.cno=1)and not exists( select * from SC SCZ where SCX.sno=SCZ.sno and SCX.sclass=SCZ.sclass and SCZ.cno=2);
1.3查询1班平均分在85分以上的同学班号、学号、姓名、性别、系、各科课程号及成绩
select SC.sclass,SC.sno,sname,ssex,Sdept,cno,grade
from SC,Student
where SC.sclass=Student.sclass and SC.sno=Student.sno and SC.sclass=1 and SC.sno in
(select sno from SC where sclass=1 group by sno having avg(grade)>85);
1.4查询至少选了1班2号同学所选课的所有班号、学号及同学姓名
select distinct SCX.sclass,SCX.sno,sname
from Student,SC SCX
where Student.sno=SCX.sno and Student.sclass=SCX.sclass and not exists(select *from SC SCYwhere SCY.sclass='1' and SCY.sno='2' and not exists (select *from SC SCZwhere SCZ.cno=SCY.cno and SCZ.sno=SCX.sno and SCZ.sclass=SCX.sclass)
1.5查询不选1号课的学生班号及学号
select Student.sclass,Student.sno
from Student
where not exists (select *from SCwhere SC.sclass=Student.sclass and SC.sno=Student.sno and SC.cno=1)
1.6查询选2号课的学生名字及相应2号课成绩,按成绩从高到低排序
select Student.sname,grade
from SC,Student
where SC.sclass=Student.sclass and SC.sno=Student.sno and cno=2
order by grade desc
1.7统计学生选修课程的班号、学号及总学分
select sclass,sno,sum(ccredit) sum_credit
from Course,SC
where Course.cno=SC.cno
group by sno,sclass
1.8统计1班选修3号课的学号及平均分
select avg(grade) avg_grade
from SC
where sclass=1 and cno=3
select sno
from SC
where sclass=1 and cno=3
1.9把个人信息及选课信息插入到Student和SC 表及新增加一门“无机化学”课程信息
insert into Course(cno,cname)values(8, '无机化学');
1.10删除选修3号课的所有选课信息并显示删除后的结果
delete from SC where cno=3;
select *
from SC;
1.11把选修1号课的所有男同学年龄增加1岁并显示最终学生Student信息
update Student
set sage=sage+1
where ssex= '男'and exists(select * from SC where cno=1 and SC.sclass=Student.sclass and SC.sno=Student.sno);
select *
from Student;
1.12把每个选课人的学号、班号及平均成绩插入到一个新表中
create table avg_grade(sno int not null,sclass int not null,avggrade float,primary key(sclass,sno)
);
insert into avg_grade
select sno,sclass,avg(grade)
from SC
group by sclass,sno;
select *
from avg_grade
2.1使用企业管理器创建视图:在ST库中以“student”表为基础,建立信息系学生的视图V_ISStudent
create view V_ISStudent AS
select *
from Student
where Sdept = 'IS' with check option;select *
from V_ISStudent
2.2
① 建立一个每个学生的学号、班号、姓名、选修的课名及成绩的视图S_C_GRADE;
create view S_C_GRADE AS
select Student.sno,Student.sclass,sname,cname,grade
from Student join SC on (Student.sclass=SC.sclass and Student.sno=SC.sno)join Course on SC.cno=Course.cno;select *
from S_C_GRADE
② 建立信息系建立信息系选修了1号课程且成绩在90分以上的学生的视图V_IS_Score
create view V_IS_Score AS
select *
from V_ISStudent
where exists(select * from SC where cno=1 and grade>90 and V_ISStudent.sno=SC.sno and V_ISStudent.sclass=SC.sclass);select *
from V_IS_Score
③ 将各系学生人数,平均年龄定义为视图V_NUM_AVG。
create view V_NUM_AVG AS
select Sdept,count(*) num,avg(sage) avg_age
from Student
group by Sdept;
select *
from V_NUM_AVG
2.3查询以上所建的视图结果
见上题
2.4查询选修了1号课程的信息系学生
select * from V_ISStudent
where exists
(select *
from SC
where cno=1 and V_ISStudent.sno=SC.sno
and V_ISStudent.sclass=SC.sclass);
2.5在信息系学生的视图中找出年龄小于20岁的学生
select *
from V_ISStudent
where sage<20;
2.6将信息系学生视图V_ISStudent中学号一班2号的学生姓名改为“刘辰”
update V_ISStudent
set sname= '刘辰'
where sclass=1 and sno=2;select sname
from V_ISStudent
where sclass=1 and sno=2
2.7用SQL语句删除视图S_C_GRADE
drop view S_C_GRADE;
SQL语言增、删、改、查功能练习相关推荐
- 表单的增 删 改 查
django单表操作 增 删 改 查 一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取 ...
- python学生姓名添加删除_python-函数-实现学生管理系统,完成对学员的增,删,改,查和退出学生管理系统。...
实现学生管理系统,完成对学员的增,删,改,查和退出学生管理系统. 要求1:使用一个list用于保存学生的姓名. 要求2:输入0显示所有学员信息,1代表增加,2代表删除,3代表修改,4代表查询,exit ...
- properties(map)增.删.改.查.遍历
import java.util.Map; import java.util.Properties; import java.util.Set;/*** properties(map)增.删.改.查. ...
- PySpark︱DataFrame操作指南:增/删/改/查/合并/统计与数据处理
笔者最近需要使用pyspark进行数据整理,于是乎给自己整理一份使用指南.pyspark.dataframe跟pandas的差别还是挺大的. 文章目录 1.-------- 查 -------- -- ...
- pyRedis - 操作指南:增/删/改/查、管道与发布订阅功能
文章目录 1 redis docker 部署与安装 2 py - redis的使用 2.1 redis的连接 2.2 常规属性查看 2.2.2 关于删除 2.3 STRING 字符串的操作 2.4 H ...
- 学生信息管理系统——JAVA 语言版(主页面+增+删+改+查+退)
学生信息管理系统 前言 一.问题分析 二.学生信息管理系统程序实现思路 三.Student类的创建 程序思路 Student类代码 四.StudentManager类的创建 程序思路 StudentM ...
- 简单的php数据库操作类代码(增,删,改,查)
数据库操纵基本流程为: 1.连接数据库服务器 2.选择数据库 3.执行SQL语句 4.处理结果集 5.打印操作信息 其中用到的相关函数有 •resource mysql_connect ( [stri ...
- list 增 删 改 查 及 公共方法
1 # 热身题目:增加名字,并且按q(不论大小写)退出程序 2 li = ['taibai','alex','wusir','egon','女神'] 3 while 1: 4 username = i ...
- Linux技术--mysql数据库增-删-改-查
# mysql 数据库 ## 数据库的操作 ### 五个单位 * 数据库服务器 Linux或者 windows * 数据库 * 数据表 * 数据字段 * 数据行 ### 连接数据库 ``` 1 ...
- Python 操作 Elasticsearch 实现 增 删 改 查
Github 地址:https://github.com/elastic/elasticsearch-py/blob/master/docs/index.rst 官网地址:https://elasti ...
最新文章
- Linux中etc目录详解
- Python元组与列表
- dj鲜生-18-发送邮件功能
- ASP.NET 5 - $.ajax post JSON.stringify(para) is null
- crontab 不能执行git命令问题备忘
- 【Vue】—Vue组件基本介绍
- java previous,Java的LinkedList的previous下一个
- 爬去图片插件_学不会你打我,一个插件爬取亚马逊数据!
- 01 Servlet Jsp 技术概述
- 斐讯k2p openwrt固件改双WAN口
- postman安装后闪退
- 【三维点云滤波】对三维点云空间数据进行滤波的matlab仿真
- 西安电子科技大学计算机学院推免生资格,西安电子科技大学计算机学院(专业学位)计算机技术保研细则...
- 使用FFmpeg合并MP4视频
- orcle plsql 列类型 BLOB与CLOB的互转,RAW
- os模块的使用方法详解
- 抢先报名 | gTech 职业大揭秘,17 日 19:30 等你来
- 某公司服务器故障导致数据库文件丢失的恢复过程
- 项目可交付成果的质量管理该怎么做?
- altera 设计--仿真--下载
热门文章
- JS获取图片的EXIF信息+纠正图片方向
- matlab示波器图形导出,(最新整理)Matlab-simulink示波器图形保存
- java8实战:使用流收集数据之toList、joining、groupBy(多字段分组)(1)
- 什么是命名数据网络NDN?
- 两点之间的最短距离是?
- 基于 Vue 和 SpringBoot 实现的博客系统(附源码)
- VMWare Workstation、GSX Server、ESX Server的区别?
- db2还原备份文件详细教程
- kube-apiserver启动时报错并且不能操作etcd
- ASP新闻发布网站(一) 首页