1. 程式人生 > >學生管理系統MS資料庫練習

學生管理系統MS資料庫練習

create database student

go
--建立表
--建立教師表
use student
go
create table teacher
(
 tno char(4) constraint pk_tno primary key,
 tname char(10) not null,
 tsex char(2) constraint uk_xb check(tsex='男' or tsex='女'),
 tbirthday datetime,
 ttitle char(10)
)
go
insert into teacher
(tno,tname,tsex,tbirthday,ttitle)
values('2011','張華','男','1982-8-2','教授')
insert into teacher
(tno,tname,tsex,tbirthday,ttitle)
values('2012','潘強','男','1978-9-5','教授')
insert into teacher
(tno,tname,tsex,tbirthday,ttitle)
values('2013','王楠','男','1972-1-7','助教')
insert into teacher
(tno,tname,tsex,tbirthday,ttitle)
values('2014','馬建','男','1989-12-3','教授')
insert into teacher
(tno,tname,tsex,tbirthday,ttitle)
values('2015','秦徐斌','男','1988-3-2','講師')
--建立學生表
use student
go
create table student
(
 sno char(10) constraint pk_sno primary key,
 sname char(10) not null,
 ssex char(2) constraint uk_stu check(ssex='男' or ssex='女'),
 sbirthday datetime,
 sscore numeric(18,0),
 classno char(8),
 st_mnt int
)
go
insert into student
values('0701011101','嚴金','男','1992-3-1',518,'07010211',0)
insert into student
values('0701011102','孫曉龍','女','1993-7-2',423,'07010211',0)
insert into student
values('0701011103','許盼盼','女','1992-8-1',432,'07010211',0)
insert into student
values('0701011104','賈思凡','女','1992-5-8',467,'07010211',0)
insert into student
values('0701011105','王贏','男','1994-3-8',398,'07010111',0)
insert into student
values('0701011106','東方紅','男','1995-9-2',240,'07010111',0)
insert into student
values('0701011107','李莉','女','1992-10-2',420,'07010111',0)
insert into student
values('0601011101','張勁','男','1987-11-12',486,'06010111',0)
insert into student
values('0601011102','金偉','男','1988-4-14',487,'06010111',0)
insert into student
values('0601011103','李健','男','1987-6-29',476,'06010111',0)
insert into student
values('0601011104','王紅青','女','1988-8-24',478,'06010111',0)
insert into student
values('0601011105','譚輝','男','1988-6-16',465,'06010111',0)
insert into student
values('0601011106','陸鳳英','女','1988-1-7',480,'06010111',0)
insert into student
values('0601011107','胡濤','男','1987-7-15',462,'06010111',0)
--建立選修課程表
use student
go
create table choice
(
 sno char(10) not null,
 cno char(7) not null,
 grade real
)
go
alter table choice
add constraint fk_choice
foreign key(sno) references student(sno)

insert into choice
values('0701011101','1001',90)
insert into choice
values('0701011101','1002',50)
insert into choice
values('0701011101','1003',70)
insert into choice
values('0701011101','1004',70)
insert into choice
values('0701011102','1001',93)
insert into choice
values('0701011102','1002',53)
insert into choice
values('0701011103','1001',90)
insert into choice
values('0701011104','1003',72)
insert into choice
values('0701011105','1003',60)
insert into choice
values('0701011106','1003',78)
insert into choice
values('0701011107','1003',50)
insert into choice
values('0701011107','1004',70)
insert into choice
values('0601011101','1002','')

--建立課程表
use student
create table course
(
 cno char(7) constraint fk_co primary key,
 cname char(30) not null,
 credits real not null
)
insert into course
values('1001','Illustrator平面設計',90)
insert into course
values('1002','Photoshop影象處理',80)
insert into course
values('1003','Dreamweaver網頁製作',70)
insert into course
values('1004','資料結構',60)

CREATE TABLE department
(
 deptno char(2) constraint pk_dept primary key,
 deptname char(20) not null
)
CREATE TABLE professional
(
 pno char(4) constraint pk_pro primary key,
 pname char(30) not null,
 deptno char(2)
)
INSERT INTO department
VALUES('01','計算機工程系')
INSERT INTO department
VALUES('02','商貿管理系')
INSERT INTO department
VALUES('03','外語系')
INSERT INTO department
VALUES('04','機電工程系')
INSERT INTO department
VALUES('05','化學工程系')
INSERT INTO department
VALUES('06','物理系')
INSERT INTO department
VALUES('11','資訊系')
INSERT INTO professional
VALUES('0101','計算機應用技術','01')
INSERT INTO professional
VALUES('0102','計算機網路技術','01')
INSERT INTO professional
VALUES('0201','物流管理','02')
INSERT INTO professional
VALUES('0202','會計','02')
INSERT INTO professional
VALUES('0301','德語','03')
INSERT INTO professional
VALUES('0302','商務英語','03')
INSERT INTO professional
VALUES('0401','模具設計與製造','04')
INSERT INTO professional
VALUES('0402','機電一體化技術','04')
INSERT INTO professional
VALUES('0501','有機化工生產技術','11')
INSERT INTO professional
VALUES('0502','精細化學品生產技術','11')


CREATE TABLE class
(
 classno char(8) constraint pk_class primary key,
 classname char(16) not null,
 pno char(4) constraint fk_class references professional(pno)
)
INSERT INTO class
VALUES('07010211','網路0711','0102')
INSERT INTO class
VALUES('06010111','計應0611','0101')
INSERT INTO class
VALUES('06020111','物流0611','0201')
INSERT INTO class
VALUES('06020211','會計0611','0202')
INSERT INTO class
VALUES('07010111','計應0711','0501')

 

--實驗7 資料查詢(3)——查詢排序與查詢結果儲存
--1. 查詢課程資訊,按課程名稱降序排序
select *
from course
order by cname desc
--2. 查詢選修了1001號課程成績非空的學生學號和成績,並按成績降序排序
select student.sno,choice.grade
from student join choice
on student.sno=choice.sno
where cno='1001' and grade<>''
order by grade desc
--3. 查詢11系學生學號、姓名和年齡,按年齡升序排序
select student.sno,sname,YEAR(getdate())-YEAR(sbirthday)
from student join class
on student.classno=class.classno
join professional
on class.pno=professional.pno
where deptno='11'
--實驗8 資料查詢(4)——查詢統計與彙總
--4. 查詢課程總數
select COUNT(*)
from course
--5. 查詢選修1001號課程的學生人數
select COUNT(*)
from student
where sno in (select sno
    from choice
    where cno='1001')
--6. 查詢被選修課程的數量
select COUNT(*)
from course
where cno in (select cno  
    from choice)
--7. 查詢選修070101班學生的平均入學成績
select AVG(sscore)
from student
where classno='07010211'
--8. 查詢070101001號學生選修課程的數量、總分以及平均分
select COUNT(*),SUM(grade),AVG(grade)
from student join choice
on student.sno=choice.sno
where student.sno='0701011101'
--9. 查詢選修1001號課程的學生人數、最高分、最低分和平均分
select COUNT(*),max(grade),MIN(grade)
from student join choice
on student.sno=choice.sno
where cno='1001'
--10. 求各個課程號和相應的選課人數
select cno,COUNT(*)
from choice join student
on choice.sno=student.sno
group by cno
--11. 依次按班級、系號對學生進行分類統計人數、入學平均分
select classno,COUNT(*),AVG(sscore)
from student
group by classno
select deptno,COUNT(*),AVG(sscore)
from student join class
on student.classno=class.classno
join professional
on class.pno=professional.pno
group by deptno
--12. 查詢選修了均分在75以上的課程號及均分
select *
from
(select sno,AVG(grade)as score
from choice
group by sno)as a
where score>'75'

--13. 查詢選修了2門以上課程的學生學號
select sno
from
(select student.sno,COUNT(*) as count
from student join choice
on student.sno=choice.sno
group by student.sno)as aa
where count>2
--14. 明細彙總年齡<20的學生,並彙總學生數量、平均年齡
select *
from student
where YEAR(getdate())-YEAR(sbirthday)<20
select COUNT(*),AVG(YEAR(getdate())-YEAR(sbirthday))
from student
--15. 按班級明細彙總成績<85分的學生,彙總學生數、均分
select classno,COUNT(*),AVG(grade)
from student join choice
on student.sno=choice.sno
where grade<'80'
group by classno
--實驗9 資料查詢(5)——連線查詢
--16. 用SQL Server形式連線查詢學生學號、姓名、性別及其所選課程編號
select student.sno,sname,ssex,cno
from student join choice
on student.sno=choice.sno
order by sno
--17. 用SQL Server形式連線查詢學生學號、姓名及其所選課程名稱及成績
select student.sno,sname,ssex,cname,grade
from student join choice
on student.sno=choice.sno
join course
on choice.cno=course.cno

--18. 查詢選修了1002課程的學生學號、姓名及1001課程成績
select student.sno,sname,cno,grade
from student join choice
on student.sno=choice.sno
where cno in('1001','1002')
--19. 查詢選修了“資料結構”課程的學生學號、姓名及課程成績
select student.sno,sname,ssex,grade
from student join choice
on student.sno=choice.sno
join course
on choice.cno=course.cno
where cname='資料結構'
--20. 用左外連線查詢沒有選修任何課程的學生學號、姓名
select student.sno,sname
from student left join choice
on student.sno=choice.sno
where choice.sno is null
--21. 用右外連線查詢選修各個課程的學生學號
select distinct student.sno
from student right join choice
on student.sno=choice.sno

--實驗10 資料查詢(6)——資料更新與子查詢
--22. 用子查詢對各班人數進行查詢(新增列)
select classno,COUNT(*)
from student
group by classno

--23. 查詢選修了1002課程成績不及格的學生的學號、姓名和性別,並按姓名升序排序
select sno,sname,ssex
from student
where sno in(select sno
   from choice
   where grade<'60' and cno='1002')
order by sname asc
--24. 查詢“東方紅”同學所在班的學生資訊,並按姓名降序排序
select *
from student
where classno=(select classno
    from student
    where sname='東方紅')
order by sname desc

--25. 將有不及格成績的學生的st_mnt值更改為3
update student
set st_mnt=3
where sno in (select sno
    from choice
    where grade<'60')
--26. 刪除5系學生的選課資訊

DELETE FROM choice WHERE sno in (select sno
        from student
        where classno in(select classno
         from class
         where pno in (select pno
            from professional
            where deptno='01')))

--1.自定義函式
--(1)建立自定義函式stu_sr,該函式可以變換出生日期欄位的顯示效果
use student
go
create function stu_sr(@stu_sb datetime) returns nvarchar(50)
as
begin
return STR(YEAR(@stu_sb))+'年'+LTRIM(STR(month(@stu_sb)))+'月'
end
go
alter function stu_sr(@stu_sb datetime) returns nvarchar(50)
as
begin
declare @sbyear nvarchar(10) =STR(YEAR(@stu_sb))
declare @sbmonth nvarchar(10) =LTRIM(STR(month(@stu_sb)))
if @sbmonth>9
 return @sbyear+'年'[email protected]+'月'
else
 return @sbyear+'年'+'0'[email protected]+'月'
 return 0
end
go
select sno,dbo.stu_sr(sbirthday) as 出生年月,sbirthday
from student
--(2)建立自定義函式kc,要求可以查詢選修了某課程的學生資訊
use student
go
if exists(select *
  from sysobjects
  where name = 'kc'
  and type='IF')
drop function kc
go
create function dbo.kc(@cname char(30)) returns table
as
return
(select student.sno
from student join choice
on student.sno=choice.sno
join course
on choice.cno=course.cno
where [email protected])
go
select *
from kc('Illustrator平面設計')
--2.儲存過程
--建立儲存過程kcgc,要求可以查詢選修了某課程的學生資訊
use student
go
if exists(select *
  from sysobjects
  where name = 'kcgc'
  and type='P')
drop function kcgc
go
create proc kcgc
@cname char(30)
as
select student.sno
from student join choice
on student.sno=choice.sno
join course
on choice.cno=course.cno
where [email protected]
go
execute kcgc'Illustrator平面設計'
--3.觸發器
use student
go
--drop trigger delete_professional
create trigger delete_professional
on professional
for delete
as
if(select COUNT(*) from class join deleted
on class.pno=deleted.pno)>0
  begin
 print('該專業在班級表中,不可刪除此記錄!')
 rollback transaction
  end
 else
  print('記錄已經刪除')
go
delete professional
where pname='計算機應用技術'