-準備工作
-- 雪梨15分大作業
-- 建庫
create database TeachingManagementSystem
use TeachingManagementSystem
--建表
create table Student --學生表
(
Sno char(3) primary key, --Sno 學號 長度3 主碼
Sname char(6) not null, --Sname 姓名 長度6 非空
Ssex char(2) check(Ssex='男' or Ssex='女'),
Sbirthday date not null, --YYYY-MM-DD
class char(5) not null --班級
)
drop table Student
create table Course
(
Cno char(5) primary key, --課程號
Cname varchar(10) not null, --課程名
Tno char(3) not null, --教師編號
foreign key(Tno) references Teacher(Tno)
)
drop table Course
create table Score
(
Sno char(3), --學號
Cno char(5), --課程號
Degree int not null --成績
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
)
drop table Score
create table Teacher
(
Tno char(3) primary key, --教師編號
Tname char(6) not null, --教師姓名
Tsex char(2) check(Tsex='男' or Tsex='女'),
Tbirthday date not null,
Prof char(6) not null, --職稱
Depart char(10) not null --學院
)
drop table Teacher
--更新初始數據
insert into Student(Sno,Sname,Ssex,Sbirthday,class)
values('108','建華','男','1977-09-01','95033');
insert into Student(Sno,Sname,Ssex,Sbirthday,class)
values('105','匡明','男','1975-10-02','95031');
insert into Student(Sno,Sname,Ssex,Sbirthday,class)
values('107','王麗','女','1976-01-23','95033');
insert into Student(Sno,Sname,Ssex,Sbirthday,class)
values('101','李軍','男','1976-02-20','95033');
insert into Student(Sno,Sname,Ssex,Sbirthday,class)
values('109','王芳','女','1975-02-10','95031');
insert into Student(Sno,Sname,Ssex,Sbirthday,class)
values('103','陳君','男','1974-06-03','95031');
insert into Course(Cno,Cname,Tno)
values('3-105','計算機導論','825');
insert into Course(Cno,Cname,Tno)
values('3-245','操作系統','804');
insert into Course(Cno,Cname,Tno)
values('6-166','數字電路','856');
insert into Course(Cno,Cname,Tno)
values('9-888','高等數學','831');
insert into Teacher(Tno,Tname,Tsex,Tbirthday,Prof,Depart)
values('804','李誠','男','1958-12-02','副教授','計算機系')
insert into Teacher(Tno,Tname,Tsex,Tbirthday,Prof,Depart)
values('856','張旭','男','1959-03-12','講師','電子工程系')
insert into Teacher(Tno,Tname,Tsex,Tbirthday,Prof,Depart)
values('825','王萍','女','1972-05-05','助教','計算機系')
insert into Teacher(Tno,Tname,Tsex,Tbirthday,Prof,Depart)
values('831','劉兵','女','1977-08-14','助教','電子工程系')
insert into Score(Sno,Cno,Degree)
values('103','3-245',86);
insert into Score(Sno,Cno,Degree)
values('105','3-245',75);
insert into Score(Sno,Cno,Degree)
values('109','3-245',68);
insert into Score(Sno,Cno,Degree)
values('103','3-105',92);
insert into Score(Sno,Cno,Degree)
values('105','3-105',88);
insert into Score(Sno,Cno,Degree)
values('109','3-105',76);
insert into Score(Sno,Cno,Degree)
values('101','3-105',64);
insert into Score(Sno,Cno,Degree)
values('107','3-105',91);
insert into Score(Sno,Cno,Degree)----
values('108','3-105',78);
insert into Score(Sno,Cno,Degree)
values('101','6-166',85);
insert into Score(Sno,Cno,Degree)
values('107','6-166',79);
insert into Score(Sno,Cno,Degree)---
values('108','6-166',81);
–第一問:
問題一:定義遊標,並實現打印出每門課程的選課人數
--聲明
declare youbiao cursor
for select Cno from Score
--打開
open youbiao
--操作
declare @num1 int,@num2 int,@num3 int,@num4 int
declare @string1 char(5)
select @num1=0,@num2=0,@num3=0,@num4=0;
fetch next from youbiao into @string1
while @@FETCH_STATUS=0
begin
if exists(select * from Course where cno=@string1)
begin
if(@string1='3-105')
set @num1=@num1+1;
if(@string1='3-245')
set @num2=@num2+1;
if(@string1='6-166')
set @num3=@num3+1;
if(@string1='9-888')
set @num4=@num4+1;
end
fetch next from youbiao into @string1
end
--關閉
close youbiao
--釋放
deallocate youbiao
--打印
select @num1 計算機導論,@num2 操作系統,@num3 數字電路,@num4 高數;
–問題2:
問題二:定義存儲過程,實現傳入班級和課程,得到這個班級這門課程的平均分並打印。(5分)
create proc cunchu
@class char(5), --班級 class
@curse char(5), --課程 cno
@num float OUT --平均分
as
begin
select @num=avg(Degree) from Score --從已知選擇平均目標
Where @curse=Cno and Sno in --確定已知數據來源範圍
(
select Sno from Student where class=@class
)
end
--打印
declare @sss float;
set @sss=0
exec cunchu '95033','3-105',@sss out
select @sss 平均分
–問題3:
問題三:定義函數,實現傳入學生學號,返回學生的選修課程表,包括:學生學號,姓名,課程名,成績,並打印表內容。(5分)
create function hanshu(@sno char(3))
returns table
as
return
(
select Student.Sno,Sname,Cname,Degree from Student,Score,Course
where Student.Sno=Score.Sno and
Score.Cno=Course.Cno and
Student.Sno=@sno
)
--打印
select Sno 學號,Sname 姓名,Cname 課程名,Degree 成績 from hanshu('108');
成就感啊,MAX