当前位置:文档之家› 数据库实验报告

数据库实验报告

武汉工程大学计算机科学与工程学院
《数据库系统》实验报告
实验内容
一、对实验一数据库,使用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);
结果:
15、查询计算机系选修了所有课程的学生的信息。

代码:
select *
from student
where not exists
(select *
from course
where not exists
(select *
from sc
where sno=student.sno
and cno=o));
结果:
16、查询计算机系没有选修‘数据库’这门课程的学生信息。

代码:
select *
from student
where not exists
(
select *
from sc,course
where sno=student.sno and ame='数据库'and o=o);
结果:
二、视图的操作
1.创建“计算机系”所有学生的视图。

并通过视图修改学生的信息。

代码:
create view student1
select *
from student
where sdept='CS';
Update student1
Set sname=’曾凯’
Where sname=’李勇’;
结果:
2.创建所有选修“数据库”这门课程的学生及成绩信息。

显示学号、姓名、系别、成绩信息。

是否能修改视图中信息。

代码:
create view student2
as
select *
from student
where sno in
(select sno
from sc,course
where o=o and ame='数据库');
结果:
视图中的信息不能修改。

相关主题