數據庫實驗(學生信息表)
目錄
實驗一
創建數據庫以及學生信息表、課程信息表、選課表
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'
實驗三
- 查詢數學系學生的學號和姓名;
select Sno,Sname from student where Sdept='MA';
- 查詢選修了課程的學生學號;
select Sno from student where Sno in (select Sno from sc)
- 查詢選修了1號課程的學生學號和成績,並要求結果按成績降序排列,如果成績相同,則按學號升序排列;
select Sno,Grade from sc where Cno='1' order by Grade desc
- 查詢選修了1號課程且成績在80-90分之間的學生學號和成績,並將成績乘以系數0.8輸出;
select Sno,Grade*0.8 from sc where Cno='1' and Grade>=80 and Grade<=90
- 查詢數學系或計算機系姓張的學生的信息;
select * from student where Sname like '張%'and Sdept in ('CS','MA')
- 查詢缺少了成績的學生的學號和課程號;
select Sno,Cno from sc where Grade is null
- 查詢每個學生的情況以及他(她)所選的課程;
select student.*,Cname from student,sc,course
where student.Sno=sc.Sno and sc.Cno=course.Cno
- 查詢學生的學號、姓名、選修的課程名稱及成績;
select student.Sno,Sname,Cname,Grade from student,sc,course
where student.Sno=sc.Sno and sc.Cno=course.Cno
- 查詢選修了“數據庫”課程且成績在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
- 查詢每門課程的間接先行課的課程名稱。
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
- 查詢至少選修了學號為“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