一、实验目的
二、实验环境
WINDOWS 10+MySQL8.0
三、实验准备
1.使用SQL命令在EDUC数据库中新建3张表,各表的结构如下所示:
- STUDENT的结构
列名 | 数据类型 | 宽度 | 空否 | 备注 |
---|---|---|---|---|
sno | char | 8 | N | 主码 |
sname | char | 8 | N | |
ssex | char | 2 | Y | 取值“男”或“女” |
sage | int | Y | ||
sdept | char | 10 | Y |
-
COURSE****的结构
列名 数据类型 宽度 空否 备注 cno char 2 N 主码 cname char 30 Y credit int Y cpno char 3 Y -
SC****的结构
列名 数据类型 宽度 空否 备注 sno char 8 N 外码 合为主码 cno char 2 N 外码 grade int Y 取值在0—100之间 -
插入数据元组
create database EDUC create table student ( sno char(8) not null primary key,#主键默认非空,not null可写可不写 sname char(8) not null, ssex char(2) check(ssex='男' or ssex='女'), sage int , sdept char(10) ); create table course( cno char(2) not null primary key, cname char(30), credit int , cpno char(3) ); create table sc( sno char(8) not null , cno char(2) not null, grade int check(grade<=100 and grade>=1), primary key(sno,cno), foreign key(sno) references student(sno), foreign key (cno) references course(cno) ); insert into student(sno,sname,ssex,sage,sdept)values('95001','李勇','男','20','CS'), ('95002','刘晨','女','19','IS'), ('95003','王敏','女','18','MA'), ('95004','张立','男','19','IS'), ('95005','刘云云','女','18','CS'); insert into course(cno,cname,credit,cpno)values('1','数据库','4','5'), ('2','数学','6',''), ('3','信息系统','3','1'), ('4','操作系统','4','6'), ('5','数据结构','4','7'), ('6','数据处理','3',''), ('7','PASCAL语言','4','6'); insert into sc(sno,cno,grade)values('95001','1','92'), ('95001','2','85'), ('95001','3','88'), ('95002','2','90'), ('95002','3','80'), ('95003','2','85'), ('95004','1','58'), ('95004','2','85');
四、实验内容
1.查询全体学生的学号和姓名
-
select sno,sname from student
2.查询全体学生的详细记录
-
select * from student
3.查询CS学院的学生姓名、年龄、系别
-
select sname,sage,sdept from student where sdept='CS'
4.查询所有选修过课程的学生学号
-
select distinct sno from sc #原因:sc是中间表,课程号和学生号都有,直接在sc表当中查询,就可以查询出参加选修课的学生学号,同一个学号可能选修过多个课程,由此需要用distinct进行学号过滤
5.查询考试不及格的学生学号
-
select distinct sno from sc where grade<60
6.查询不是CS学院的学生姓名、年龄、系别 (三种方法实现)
-
select sname,sage,sdept from student where sdept!='CS' --不相关子查询 select sname,sage,sdept from student where sno not in(select sno from student where sdept='CS') --相关子查询 select sname,sage,sdept from student s1 where exists (select * from student s2 where s1.sno=s2.sno and s2.sdept!='CS')
7.查询年龄在19-20岁的学生学号、姓名、系别、年龄
-
select sno,sname,sdept,sage from student where sage between 19 and 20
8.查询姓刘的学生情况
-
select * from student where sname like '刘%'
9.查询姓刘或姓李的学生情况 (注意不同的写法)
-
select * from student where sname like '刘%' or sname like '李%' select * from student where sname like '[刘李]%'
10.查询姓刘且名字为两个字的学生情况
-
select * from student where sname like '刘_'
11.查询1999年以后出生的学生姓名
-
select sname,year(getdate())-sage birth from student where year(getdate())-sage>1999
12.利用内部函数year()查询CS学院学生的出生年份
-
select sname,year(getdate())-sage birth from student where sdept='CS'
13.查询全体学生情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列。
-
select * from student order by sdept,sage desc
14.查询学生总人数
-
select count(*) xueshengrenshu from student
15.查询选修了课程的学生人数(两种方法实现)
-
select count(distinct sno) xuanxurenshu from sc -- 不相关子查询(没意义) select count(*)xuanxurenshu from student where sno in (select distinct sno from sc) --相关子查询 select count(*)xuanxiurenshu from student where exists (select * from sc where sc.sno=student.sno)
16.查询选修了2号课程的学生总人数和平均成绩
-
select count(sno) erhaorenshu,avg(grade) average from sc where cno='2'
17.查询选修2号课程学生的最好成绩
-
select max(grade) zuihao from sc where cno='2'
18.查询每个系的系名及学生人数
-
select sdept,count(*) renshu from student group by sdept
19.查询每门课的选修人数及平均成绩
-
select cno,count(*) renshu,avg(grade) from sc group by cno
20.查询没有被选修的课程信息(两种方法实现)
-
select * from course where cno not in (select distinct cno from sc) select * from course where not exists (select * from sc where course.cno=sc.cno )