数据库实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
武汉工程大学计算机科学与工程学院
《数据库系统》实验报告
实验内容
一、对实验一数据库,使用oem完成下列各SQL语句。
1、用CREATE语句创建学生表、课程表、选课表(要求定义主码、外码)。参照85页创建Student表:
create table student
(sno char(10) not null,
sname char(8) not null,
ssex char(2) check(ssex in ('男','女')) not null,
sage int,
sdept varchar(20),
primary key(sno)
);
创建Course表:
create table course
(cno int not null,
cname char(20) not null,
cpno int,
ccredit int,
primary key(cno)
);
创建SC表:
create table sc
(sno char(10) not null,
cno int not null,
grade int,
primary key(sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
);
2、在上述三个表中用SQL语句插入记录。记录内容参见教材中的表的记录内容。Course表
数据插入之后结果:
3、将STUDENT表中学号为95001的年龄加1。代码:
update student
set sage=sage+1
where sno='95001'
/
结果:
4、将刘晨同学的2号课程的成绩改为80分。
代码:
Update sc
Set grade=80
Where cno=2 and sno=
(select sno
From student
Where student.sname='刘晨'
)
/结果:
5、在SC表中删除刘晨同学的记录。
代码:
delete
from sc
where sno=
(select sno
from student
where student.sname='刘晨'
);
结果:
6、查询‘IS’系的所有学生的信息。
代码:
select *
from student
where sdept='IS';
结果:
7、查询所有姓‘王’的学生的详细信息。代码:
select *
from student
where sname like '王%';
结果:
8、查询所有年龄在19到25之间的学生的详细信息。并按年龄由低到高的顺序排列。
代码:
select *
from student
where sage>=19 and sage<=25;
结果:
9、查询选修了课程的学生的学号及姓名。
代码:
select sno,sname
from student
where sno in
(
select sno
from sc
);
10、查询所有选修‘信息系统’这门课程的学生成绩信息,显示学号、姓名、成绩,并按成绩的高低顺序排列。
代码:
select student.sno,student.sname,sc.grade
from student,sc
where sc.sno=student.sno and o in
(
select cno
from course
where cname='信息系统'
)
order by grade;
/
结果:
11、按系统计男、女生人数。
代码:
select sdept 班级,count(case when ssex='男' then 1 end) as 男,
count(case when ssex='女' then 1 end) as 女
from student
group by sdept;
结果:
12、查询每门课程的最高分、最低分及平均分。
代码:
select cname,max(distinct grade) as 最高分,min(distinct grade) as 最低分from sc,course
where o=o
group by cname;
结果:
13、查询选修了1门以上课程的学生的学号、姓名及课程门数。
代码:
select sc.sno,student.sname,count(sc.sno) as 课程数
from student,sc
where student.sno=sc.sno
group by sc.sno,student.sname;
结果:
14、查询既选修了‘信息系统’,又选修‘数学’这两门课程的学生的详细信息。代码:
select *
from student
where sno in
(select sno
from sc,course
where ame='数学'and o=o)
union
select *
from student
where sno in
(select sno
from sc,course
where ame='信息系统'and o=o);
结果: