博客
关于我
sql综合2
阅读量:82 次
发布时间:2019-02-26

本文共 3853 字,大约阅读时间需要 12 分钟。

针对学生课程选课数据库进行以下操作

学生表:

创建表:

create table t_student(

Sno CHAR(7) primary key,

Sname CHAR(10) ,

Ssex CHAR(2),

Sage int DEFAULT 20,

Sdept CHAR(20)

);

插入数据:

insert into t_student(Sno,Sname,Ssex,Sage,Sdept)

values(“0804601”,”lily”,”m”,12,”jk”),

(“0804602”,”mary”,”f”,17,”jk19”),

(“0804603”,”bob”,”m”,18,”jk18”),

(“0804604”,”hani”,”m”,10,”jk”),

(“0804605”,”job”,”m”,13,”jk”);

image-20210429164838098

课程表

创建表

create table t_course(

Cno Char(10) PRIMARY KEY,

Cname Char(20) NOT NULL,

Ccredit int CHECK(Ccredit>0),

Semester int CHECK(Semester>0),

Period int CHECK(Period>0),

);

mysql不支持check,

插入数据

insert into t_course(Cno,Cname,Ccredit,Semester,Period)

values(“TX4601”,”sjk”,3,1,2),

(“TX4602”,”java”,4,2,2),

(“TX4603”,”jw”,3,1,2),

(“TX04604”,”web”,2,1,2),

(“TX04605”,”mks”,2,1,2);

image-20210429164911730

学生选课表

创建表:

create table t_SC(

Sno CHAR(7) NOT NULL,

Cno CHAR(10) NOT NULL,

Grade int,

PRIMARY KEY (Sno,Cno),

FOREIGN KEY(Sno) REFERENCES t_student(Sno),

FOREIGN KEY(Cno) REFERENCES t_course(Cno)

);

插入数据:

insert into t_SC(Sno,Cno,Grade) values(“0804601”,”TX4601”,99),

(“0804602”,”TX4602”,98),

(“0804603”,”TX4603”,97),

(“0804604”,”TX4604”,96),

(“0804605”,”TX4605”,95);

image-20210429164959330

1、以Cno升序、Grade降序查询Sc表的所有记录。

select * from t_SC order by Cno asc,Grade desc;

image-20210429164741952

2、查询所有学生的Sname、Cname和Grade列。(连接查询,嵌套查询)

select Sname,Cname,Grade from t_student s,t_course c,t_sc sc where s.Sno=sc.Sno and sc.Cno=c.Cno;

image-20210429170335321

3、查询所有选修“计算机导论”课程的同学的成绩。(连接查询,嵌套查询)

此处查询的课程名为jw

select sname,grade from t_sc sc,t_student s,t_course c where c.Cname=“jw” and sc.cno=c.cno and s.Sno=sc.Sno;

image-20210429171420484

4、查询和“李军”同性别的同学Sname. (自身连接查询,嵌套查询)

此处查询的同学姓名为lily

select Sname from t_student s where s.ssex=(select ssex from t_student where sname=“lily”) ;

image-20210429172134848

5、查询所有同学的基本情况和选课情况,包括未选课的同学。(外连接查询)

select s.Sno,Sname,Ssex,Sage,Sdept,Grade,c.Cno,Cname,Ccredit,Semester,Period

from t_student s

right join t_sc sc

on s.sno=sc.sno

right join t_course c

on sc.cno=c.cno;

image-20210429175111817

6、查询选修c05号课程且成绩高于80分的同学的名字。(连接查询,嵌套查询,集合查询)

select sname from t_student s join t_sc sc on s.sno=sc.sno join t_course c on sc.cno=c.cno and c.cno = “TX4601”where grade>80 ;

image-20210429175725421

7、查询和学号为0608002的同学同年出生的所有学生的Sno、Sname列。(自身连接查询,嵌套查询)

select distinct a.sno,a.sname from t_student a join t_student b on a.sage=(select sage from t_student where Sno=“0804601”);

image-20210429180338387

8、查询王位同学所有的成绩。(连接查询,嵌套查询)

select sname,grade from t_student s join t_sc sc on s.sno=sc.sno join t_course c on sc.cno=c.cno where s.sname=“lily” ;

image-20210429180556628

9、查询非计算机系的不超过计算机系所有学生的年龄的学生姓名。(用ANY,ALL)

由于数据过少,出现空值。

select sname from t_student a where a.sage<any(select sage from t_student where sdept = “jk”) and a.sdept!=“jk”;

image-20210429183147134

10、查询存在有85分以上成绩的课程Cno.(用exists)

select distinct sc.Cno from t_sc sc join t_course c on exists(select grade from t_sc) where sc.grade>85;

image-20210429185149392

11、查询计算机系同学的人数。

select count(sno) from t_student where sdept=“jk”;

image-20210429185510767

为方便做题,在此插入几条数据。

insert into t_sc(Sno,Cno,Grade) values

(“0804601”,”TX4602”,55),

(“0804601”,“TX4603”,66),
(“0804603”,”TX4602”,77);

12、查询数学系统学所选课程的平均分。(java)

select avg(grade) from t_sc sc join t_course c on sc.cno= c.cno where c.cname=“java”;

image-20210429192411405

13、查询Student表中年龄最大和最小的同学的具体情况。

select * from t_student where sage>=all(select sage from t_student) or sage<=all(select sage from t_student);

image-20210429191547046

14、查询最高分同学的Sno、Cno列

select Sno,Cno from t_sc where grade>=all(select grade from t_sc);

image-20210429191927076

15、查询c08号课程的平均分。

select avg(grade) from t_sc sc where sc.cno=“TX4602”;

image-20210429192526156

16、查询选修了c08号课程且成绩比该课程平均成绩低的同学的学号和成绩。

select sno,grade from t_sc sc where(select avg(grade) from t_sc sc where sc.cno=“TX4602”)>grade and sc.cno=“TX4602”;

image-20210429193126284

17、查询选修了c06号课程或1号课程的同学的学号。(复合条件查询,集合查询)

select sno from t_sc sc where sc.cno=“TX4602” or sc.cno=“TX.4603”;

image-20210429193404306

18、查询各个课程号及相应的选课人数。

select cno,count(*) from t_sc group by cno ;

image-20210429194232275

19、查询选修了3门以上课程且总分大于200分的同学的学号。

select sno from t_sc where sno=(select sno,count() from t_sc group by sno having count()>2) group by sno having sum(grade)>200;

image-20210429200746785

20、查询比自己的平均分高的课程的课程号。

select cno from t_sc where (select avg(grade) from t_sc sc where sno=“0804601”)<grade and sno=“0804601”;

image-20210429203357647

转载地址:http://enmz.baihongyu.com/

你可能感兴趣的文章
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>