《数据库原理与应用》实验二

题目来源学校课后作业,禁商业用途,仅用于课业学习

《数据库原理与应用》实验二

一、实验目的及要求


1、掌握SQL查询语言的使用。
2、掌握SQL流程控制语句的使用。
3、要求独立完成,并记录SQL语句。

二、实验环境及相关情况(包含使用软件、实验设备、主要仪器及材料等)

计算机操作系统要求在windows 7以上,并要求MySQL软件5.7后版本。

软件:Navicat 15

三、实验内容及要求

请使用实验指导书中的关系数据库模型完成下面的内容。

该数据库代码为教师提供,不在网络共享源代码,仅共享题目+本人的作业代码(复习后会对代码进行一定改进)

1、写出下面SQL语句实现的功能

SELECT COUNT(*) FROM student WHERE mno='100165' AND ssex='女';

统计表student中mno为100165和ssex为女的数量(专业为100165的女生数量)

SELECT DISTINCT SUBSTRING(sname,1,1) FROM student;

截取表student中的sname第一位开始一个长度的子字符串并去重(获得表student所有的姓)

SELECT sno,sname,ssex,mno FROM student WHERE mno IN ('100165','201148','100838');

选取表student中的mno为100165或201148或100838的sno、sname、ssex、mno数据(专业为100165、201148、100838的学生)

SELECT ccno,mark FROM student_course WHERE sno='100212201' AND mark>ANY(SELECT mark FROM student_course WHERE sno='100212208');

选取表student_course中的sno为100212201和mark大于任意一个sno为100212208的分数的ccno、mark的数据(学号为100212201的学生的课程成绩大于学号为100212208的学生的最低分数的课程)

SELECT DISTINCT s.sno,sname,dname FROM student AS s,department AS d,student_course AS sc,major AS m WHERE s.mno=m.mno and d.dno=m.dno and s.sno=sc.sno and mark < 60;

从student中的mno等于major中的mno、department中的dno等于major的dno、student中的sno等于student_course的sno、mark小于60的数据中选取student的sno,sname,dname去重后的数据(每个系成绩小于60的学生)

SELECT sno,sname,mname FROM student AS s,major AS m WHERE s.mno=m.mno AND s.sno IN (SELECT DISTINCT sno FROM student_course WHERE mark < 60);

从student中的mno等于major中的mno、student的sno为student_course中mark小于60的sno去重后的数据选取sno、sname、mname的数据(每个专业成绩小于60的学生)

2、根据下面的要求,写出相应的查询语句

(1)查询所有男同学的选课情况,要求列出学号、姓名、开课号、分数。

SELECT student.sno AS '学号',sname AS '姓名',ccno AS '开课号',mark AS '分数' FROM student,student_course WHERE ssex = '男';

(2)创建一个视图显示所有学生的总成绩,最高成绩,要求列出学号、总成绩和最高成绩。

CREATE VIEW fenshu AS SELECT student.sno AS '学号',sum( mark ) AS '总成绩' ,max( mark ) AS '最高成绩' FROM student,student_course WHERE student.sno=student_course.sno GROUP BY student.sno;

创建视图、表都需要另行语句调用才会展现。

SELECT * FROM fenshu;

(3)查询出姓张的学生或者姓名中带有“秋”的学生

SELECT * FROM student WHERE sname like '张%' or sname like '%秋';

(4)查询出每门课程的平均分、最低分、最高分

SELECT avg(mark) as'平均分',min(mark) as '最低分',max(mark) as '最高分' FROM student_course GROUP BY ccno;

(5)查询出平均分大于80分,且至少选修了2门课程的学生学号。

SELECT student.sno AS '学号' FROM student,student_course WHERE student.sno=student_course.sno GROUP BY student.sno having avg(mark)>80 AND count(ccno)>=2;

(6)求选修课程号为'010104'且成绩在90以上的学生学号、姓名和成绩

SELECT s.sno AS '学号',sname AS '姓名',mark AS '成绩' FROM student AS s,student_course AS sc,course_class AS cc WHERE s.sno=sc.sno AND cc.ccno=sc.ccno AND mark >=90 AND cno='010104';

(7)创建一个视图显示每一门课程的间接先行课(即先行课的先行课)

CREATE VIEW kecheng AS SELECT c1.cname AS '课程',c2.cname AS '间接先行课' FROM course AS c1 LEFT JOIN course AS c2 ON c1.cpno=c2.cno;
SELECT * FROM kecheng;

(8)求高等数学课程的成绩高于刘晨的学生学号和成绩

SELECT s.sno AS '学号',mark AS '成绩' FROM student AS s,student_course AS sc,course AS c,course_class AS cc WHERE s.sno=sc.sno AND sc.ccno=cc.ccno AND cc.cno=c.cno AND cname='高等数学' AND mark>(SELECT mark from student AS s,student_course AS sc,course AS c,course_class AS cc WHERE s.sno=sc.sno AND sname='刘晨' AND sc.ccno=cc.ccno AND cc.cno=c.cno AND cname='高等数学');

(9)求其他系中比计算机系某一学生年龄小的学生(即年龄小于计算机系年龄最大者的学生)

SELECT sname AS '姓名' FROM student AS s,major AS m,department AS dp WHERE s.mno=m.mno AND m.dno=dp.dno AND dname not in ('计算机系') AND TIMESTAMPDIFF(YEAR,sbirth,CURDATE())<(SELECT max(TIMESTAMPDIFF(YEAR,sbirth,CURDATE())) FROM student AS s,major AS m,department AS dp WHERE s.mno=m.mno AND m.dno=dp.dno AND dname='计算机系');

(10)查询同时选修了“数据库原理与应用”“金融学”“运筹学”三门课程的学生姓名。(要求使用EXISTS)

SELECT s.sname AS '姓名' FROM student AS s where EXISTS( SELECT * FROM student_course AS sc,course_class AS cc,course AS c where sc.sno=s.sno and sc.ccno=cc.ccno AND cc.cno=c.cno AND c.cname in('数据库原理与应用','金融学','运筹学') GROUP BY s.sname having count(cname)=3 );

(11)查询同时选修了“数据库原理与应用”“金融学”“运筹学”三门课程的学生姓名。(要求不能使用EXISTS)

SELECT s.sname AS '姓名' FROM student AS s,student_course AS sc,course_class AS cc,course AS c where s.sno=sc.sno AND sc.ccno=cc.ccno AND cc.cno=c.cno AND cname in('数据库原理与应用','金融学','运筹学') GROUP BY s.sname having count(cname)=3;

3、思考题

   (1)如何求出“金融学”成绩排名第5到第10之间的学生姓名。

SELECT s.sname AS '姓名' FROM student AS s,student_course AS sc,course_class AS cc,course AS c where s.sno=sc.sno AND sc.ccno=cc.ccno AND cc.cno=c.cno AND c.cname='金融学' ORDER BY mark desc limit 4,5;

   (2)假设有两个结构完全相同的表,一个是运营数据表A,一个是历史数据表B,由于操作失误导致两个表中出现了部分完全相同的数据,请将完全相同的数据从A中删除?

create table A
(
id VARCHAR(6) PRIMARY KEY,
bname VARCHAR(16) NOT NULL
);
INSERT INTO A VALUES('1','龙傲天'),('2','河堤'),('3','杨过');
create table B
(
id VARCHAR(6) PRIMARY KEY,
gname VARCHAR(16) NOT NULL
);
INSERT INTO B VALUES('3','杨过'),('4','玛丽苏'),('5','兰花');
DELETE A FROM A INNER JOIN B ON B.id = A.id;

时间:2022年10月23日

如有错误,请于评论区指正。

你可能感兴趣的:(数据库原理与应用,数据库,sql,mysql)