一.第一周笔记汇总
《第一周周末作业》
二.数据库作业
1.完成两道练习题,用nvicat创建数据库和表以及数据,写出正确的SQL查询语句和运行结果截图。
《第一周周末作业》
解:创建的表及表中数据如下图
《第一周周末作业》
《第一周周末作业》
1)1) select avg(english) “平均成绩” from mark;
《第一周周末作业》
2) select b.math,a.Name,a.Address,a.Telno from cust a join mark b on a.Studentno=b.Studentno where a.Studentno in (11,22,33,44,55);(因为表中没有学号为11,22,33,44,55,66的数据,所以查询结果为null)
《第一周周末作业》
3)select a.Name,b.computer from cust a join mark b on a.Studentno=b.Studentno order by b.computer desc;
《第一周周末作业》
4)select a.Studentno,a.name,(b.computer+b.english+b.math) “总成绩” from cust a join mark b on a.Studentno=b.Studentno where (b.computer+b.english+b.math)>240 order by (b.computer+b.english+b.math) desc;
《第一周周末作业》
《第一周周末作业》
解:创建的表及表中的数据如下图
《第一周周末作业》
《第一周周末作业》
《第一周周末作业》
A. select count(*) from student where name like “王%”;
《第一周周末作业》
B. select id from (select a.id,max(case when c.name=‘语文’ then b.score end) chinese,max(case when c.name=‘数学’ then b.score end) math from student a join sc b on a.id=b.sid join course c on c.id=b.cid group by a.id having math>chinese) biao;
《第一周周末作业》
C. select a.name,avg(b.score) ‘平均成绩’ from student a join sc b on a.id=b.sid join course c on c.id=b.cid group by a.name having avg(b.score) > 90;
《第一周周末作业》
2.用SQL语句创建以下三张表,要求字段属性要求如图所示。(用nvicat),用SQL语句,分别在三张表中插入数据。注意:学生表的id等于成绩表的sid,班级表的id等于学生表的cid,注意,以上题目,请把sql语句写出来。
学生表:t_student
《第一周周末作业》
班级表:t_class
《第一周周末作业》
成绩表:t_grade《第一周周末作业》
表数据如下图
《第一周周末作业》
《第一周周末作业》
《第一周周末作业》
1) 用SQL语句对班级表学生表进行多表联查,显示班级表的班级名称和学生的id,姓名
select b.cname,a.id,a.sname from t_student a join t_class b on a.cid=b.id;
《第一周周末作业》
2) 用SQL语句对学生表成绩表多表联查,显示学生的id,sname和成绩表的语数外的成绩。
select a.id,a.sname,b.chinese,b.math,b.english from t_student a join t_grade b on a.id=b.sid;

《第一周周末作业》
3) 用SQL语句,对三张表进行多表联查,显示学生表的id,sname班级表的cname,成绩表的语数外成绩。
select a.id,a.sname,c.cname,b.chinese,b.math,b.english from t_student a join t_grade b on a.id=b.sid join t_class c on a.cid=c.id;
《第一周周末作业》

相关文章: