欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据库实验(学生信息表)

程序员文章站 2022-06-04 08:47:42
...

数据库实验(学生信息表)

实验一

创建数据库以及学生信息表、课程信息表、选课表

create Table student
(Sno char(9) primary key,
Sname char(20) unique,
Ssex char(2),
Sage smallint,
Sdept char(20)
)

create table course
(Cno char(4) primary key,
Cname char(40),
Cpno char(4),
Ccredit smallint,
foreign key (Cpno) references course(Cno),
)

create table sc
(Sno char(9),
 Cno char(4),
 Grade smallint,
 primary key(Sno,Cno),
 foreign key (Sno) references student(Sno),
 foreign key (Cno) references course(Cno),
)

实验二

(1)分别向三个表中插入以下数据

Sno Sname Ssex Sage Sdept
95001 李勇 30 CS
95002 刘晨 19 IS
95003 王敏 18 MA
95004 张立 19 IS
Cno Cname Cpno Ccredit
1 数据库 5 4
2 数学 2
3 信息系统 1 4
4 操作系统 6 3
5 数据结构 7 4
6 数据处理 2
7 C语言 6 4
Sno Cno Grade
95001 1 32
95001 2 85
95001 3 88
95002 2 90
95002 3 80

语句如下:

insert into student values('201215121','李勇','男',20,'CS')
insert into student values('201215122','刘晨','女',19,'CS')
insert into student values('201215123','王敏','女',18,'MA')
insert into student values('201215124','张立','男',19,'IS')

insert into course values('1','数据库','5',4)
insert into course values('2','数学',null,2)
insert into course values('3','信息系统','1',4)
insert into course values('4','操作系统','6',3)
insert into course values('5','数据结构','7',4)
insert into course values('6','数据处理',null,2)
insert into course values('7','pascal',6,4)

insert into sc values('201215121','1',92)
insert into sc values('201215121','2',85)
insert into sc values('201215121','3',88)
insert into sc values('201215122','2',90)
insert into sc values('201215122','3',80)

(2)执行完这些操作之后可以用select * 语句分别查询三张表总览图

select * from student
select * from sc
select * from course

3)修改数据

将表Student 中所有学生的年龄加2岁。 将表SC 中所有学生的成绩降低10%。

update student set Sage=Sage+2;
update sc set Grade=Grade*0.9 where Sno in (select Sno from sc)

4)删除数据
将表Student 中Sno 为95004的学生信息删除。

delete from student where Sno='95004'

实验三

  1. 查询数学系学生的学号和姓名;
select Sno,Sname from student where Sdept='MA';
  1. 查询选修了课程的学生学号;
select Sno from student where Sno in (select Sno from sc)
  1. 查询选修了1号课程的学生学号和成绩,并要求结果按成绩降序排列,如果成绩相同,则按学号升序排列;
select Sno,Grade from sc where Cno='1' order by Grade desc
  1. 查询选修了1号课程且成绩在80-90分之间的学生学号和成绩,并将成绩乘以系数0.8输出;
select Sno,Grade*0.8 from sc where Cno='1' and Grade>=80 and Grade<=90
  1. 查询数学系或计算机系姓张的学生的信息;
select * from student where Sname like '张%'and Sdept in ('CS','MA')
  1. 查询缺少了成绩的学生的学号和课程号;
select Sno,Cno from sc where Grade is null
  1. 查询每个学生的情况以及他(她)所选的课程;
select student.*,Cname from student,sc,course 
where student.Sno=sc.Sno and sc.Cno=course.Cno
  1. 查询学生的学号、姓名、选修的课程名称及成绩;
select student.Sno,Sname,Cname,Grade from student,sc,course 
where student.Sno=sc.Sno and sc.Cno=course.Cno
  1. 查询选修了“数据库”课程且成绩在90分以上的学生学号、姓名和成绩;
select student.Sno,Sname,Grade from student,sc,course 
where student.Sno=sc.Sno
and sc.Cno=course.Cno and course.Cname='数据库' and Grade>=90
  1. 查询每门课程的间接先行课的课程名称。
select first.Cno,second.Cpno from course first,course second 
where first.Cpno=second.Cno and second.Cpno is not null

2.对学生-课程数据库,应用嵌套查询实现以下查询要求:

1) 查询选修了“高等数学”的学生学号和姓名;

第一种
select student.Sno,Sname from student where Sno in
(select Sno from sc where Cno in
(select Cno from course where Cname='数学'))
第二种
 select student.Sno,Sname from student,sc,course 
 where student.Sno=sc.Sno and sc.Cno=course.Cno
 and course.Cname='数学'

2) 查询“高等数学”的成绩高于张三的学生学号和成绩;

select student.Sno,Grade from student,sc,course where
student.Sno=sc.Sno and sc.Cno=course.Cnoand course.Cname='数学'
and Grade>(select Grade from sc where Sno in
(select Sno from student where Sname ='张三')
and Cno in (select cno from course where Cname='数学'))

3) 查询其他系中年龄小于计算机系年龄最大者的学生;

select student.* from student where Sdept <> 'CS'
and Sage<(select MAX(Sage) from student where Sdept='CS')

4) 查询其他系中比计算机系学生年龄都小的学生;

select student.* from student where Sdept <> 'CS'
and Sage<(select min(Sage) from student where Sdept='CS')

5) 查询选修了“信息系统”课程的学生姓名;

select Sname from student where Sno in(select Sno from sc 
where Cno in(select Cno from course where Cname='信息系统'))

6) 查询没有选修“信息系统”课程的学生姓名;

select Sname from student where Sno  not in(select Sno from sc 
where Cno in(select Cno from course where Cname='信息系统'))

7) 查询选修了全部课程的学生姓名;

select Sname from student where Sno IN
(select Sno from SC group by Sno 
 having count(*) = (select count(*) from course ))
//根据Sno分组,统计每个学生选修课程数。如果等于course的总数,就是我们要找的Sno
  1. 查询至少选修了学号为“95002”的学生所选修的全部课程的学生学号和姓名
select student.Sno,Sname from  ( select Sno, COUNT(Cno) as num
from SC where Cno in ( select Cno from student join SC on
student.Sno = sc.Sno and sc.Sno ='95002')group by Sno ) t2 
join(select COUNT(Cno) num from student join sc on
student.Sno = sc.Sno and student.Sno ='95002' ) t1 on
t2.num = t1.num join student on student.Sno = t2.Sno

实验四

1)建立男学生的视图(Male_Student),属性包括学号、姓名、选修课程名和成绩;

create view  Male_student as
select student.Sno,Sname,sc.Cno,Grade from student,course,sc 
where student.Sno=sc.Sno and course.Cno=sc.Cno and Ssex='男'

2)在男学生视图中查询平均成绩大于80分的学生学号与姓名;

select Sno,Sname from Male_student where Grade>80

3)对男学生视图的数据进行修改;
将“95001”学号的学生姓名改为“李咏”。

update Male_student set Sname='李咏' where Sno='95001'
update Male_student set Grade=95 where Sno='95001'
and Cno=(select Cno from course where Cname='数据库')

4) 将“95001”学生选修“数据库”的成绩改为“95”。

select AVG(Grade) as avg,COUNT(Cno) as coursenumber from sc
update Male_student set Sname='李咏' where Sno='95001'
update Male_student set Grade=95 where Sno='95001'
and Cno=(select Cno from course where Cname='数据库')

5)统计每个学生有成绩的课程门数、平均成绩。

select AVG(Grade) as avg,COUNT(Cno) as coursenumber from sc
where Grade is not null group by Cno
相关标签: 数据库实验