900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 查询选修c语言课程的学生学号和姓名 数据库实验(学生信息表)

查询选修c语言课程的学生学号和姓名 数据库实验(学生信息表)

时间:2023-07-22 14:21:58

相关推荐

查询选修c语言课程的学生学号和姓名 数据库实验(学生信息表)

目录

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

实验一

实验二

实验三

实验四

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

实验一

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

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

语句如下:

```sql

insert into student values('15121','李勇','男',20,'CS')

insert into student values('15122','刘晨','女',19,'CS')

insert into student values('15123','王敏','女',18,'MA')

insert into student values('15124','张立','男',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('15121','1',92)

insert into sc values('15121','2',85)

insert into sc values('15121','3',88)

insert into sc values('15122','2',90)

insert into sc values('15122','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';

2) 查询选修了课程的学生学号;

select Sno from student where Sno in (select Sno from sc)

3) 查询选修了1号课程的学生学号和成绩,并要求结果按成绩降序排列,如果成绩相同,则按学号升序排列;

select Sno,Grade from sc where Cno='1' order by Grade desc

4) 查询选修了1号课程且成绩在80-90分之间的学生学号和成绩,并将成绩乘以系数0.8输出;

select Sno,Grade*0.8 from sc where Cno='1' and Grade>=80 and Grade<=90

5) 查询数学系或计算机系姓张的学生的信息;

select * from student where Sname like '张%'and Sdept in ('CS','MA')

6) 查询缺少了成绩的学生的学号和课程号;

select Sno,Cno from sc where Grade is null

7) 查询每个学生的情况以及他(她)所选的课程;

select student.*,Cname from student,sc,course

where student.Sno=sc.Sno and o=o

8) 查询学生的学号、姓名、选修的课程名称及成绩;

select student.Sno,Sname,Cname,Grade from student,sc,course

where student.Sno=sc.Sno and o=o

9) 查询选修了“数据库”课程且成绩在90分以上的学生学号、姓名和成绩;

select student.Sno,Sname,Grade from student,sc,course

where student.Sno=sc.Sno

and o=o and ame='数据库' and Grade>=90

10) 查询每门课程的间接先行课的课程名称。

select o,second.Cpno from course first,course second

where first.Cpno=o 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 o=o

and ame='数学'

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

select student.Sno,Grade from student,sc,course where

student.Sno=sc.Sno and o=oand ame='数学'

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

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

select student.* from student where Sdept <> 'CS'

and Sage

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

8) 查询至少选修了学号为“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,o,Grade from student,course,sc

where student.Sno=sc.Sno and o=o 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。