第六章上機練習

2021-07-27 10:29:24 字數 4779 閱讀 6243

練習1:批量插入學生考試成績

begin transaction

declare @errorsum int

set @errorsum=0

/*--插入資料--*/

insert into result(studentno,subjectno,examdate,studentresult)

values(1,1,getdate(),90)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(1,1,getdate(),70)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(2,1,getdate(),67)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(2,1,getdate(),55)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(3,1,getdate(),102)--分數違反約束

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(3,4,getdate(),90)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(4,4,getdate(),56)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(4,4,getdate(),88)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(5,4,getdate(),40)

set @errorsum=@errorsum+@@error

insert into result(studentno,subjectno,examdate,studentresult)

values(5,4,getdate(),65)

set @errorsum=@errorsum+@@error

/*--根據是否有錯誤,確定事務是提交還是撤銷--*/

if(@errorsum<>0) --如果有錯誤

begin

print '插入失敗,回滾事務'

rollback transaction

end 

else

begin

print '插入成功,提交事務'

commit transaction  

endgo

練習2:辦理畢業學生離校手續

begin transaction

declare @errorsum int

set @errorsum=0

/*--查詢result表中所有y2學員的考試成績,儲存到新錶historeresult*/

select result.* into historeresult

from result inner join student on result.studentno=student.studentno

inner join grade on grade.gradeid=student.gradeid

where gradename='y2'

set @errorsum=@errorsum+@@error

/*--刪除result表中所有y2學員的考試成績*/

delete  result from  result join student on result.studentno=student.studentno

inner join grade on grade.gradeid=student.gradeid

where gradename='y2'

set @errorsum=@errorsum+@@error

/*--將student表中所有y2的學員記錄,儲存到新錶historystudent*/

select student.* into historystudent

from student inner join grade on grade.gradeid=student.gradeid

where gradename='y2'

set @errorsum=@errorsum+@@error

/*--刪除studet表中所有y2學員記錄*/

delete student from student

inner join grade on grade.gradeid=student.gradeid

where gradename='y2'

set @errorsum=@errorsum+@@error

/*--根據是否有錯誤,確定事務是提交還是撤銷--*/

if (@errorsum<>0) --如果有錯誤

begin

print '插入失敗,回滾事務'

rollback transaction

end 

else

begin

print '插入成功,提交事務'

commit transaction  

endgo

練習3:檢視各學生各學期總成績

create view vw_student_result_info

asselect 姓名=studentname,學號=student.studentno,

聯絡**=phone,學期=gradename,成績=total

from student

left outer join (

select r.studentno,gradename,sum(studentresult) total

from result r

inner join (

select studentno,subjectno,max(examdate)  examdate

from result

group by studentno,subjectno) tmp

on r.examdate=tmp.examdate

and r.subjectno = tmp.subjectno and r.studentno = tmp.studentno

inner join subject sub on sub.subjectno = r.subjectno

inner join grade g on g.gradeid = sub.gradeid

group by r.studentno,gradename ) tmpresult2

on student.studentno = tmpresult2.studentno

group by studentname,student.studentno,phone,gradename,total

goselect * from vw_student_result_info

練習4:使用索引查詢學生考試成績

1.use myschool 

2.go 

3. 

4.if exists (select name from sysindexes 

5.          where name = 'ix_result_studentresult') 

6.   drop index result.ix_result_studentresult  --刪除索引 

7.go 

8. 

9.--考試成績列上建立可重複索引:填充因子為70%-- 

10.create index ix_result_studentresult 

11.   on result(studentresult) 

12.       with fillfactor = 70 

13.go 

14. 

15. 

16.--查詢輸出學生姓名、課程名稱、考試日期和考試成績  

17.select studentname '學生姓名',subjectname '課程名稱',examdate '考試日期', studentresult '考試成績' from subject,student,result 

18.with (index = ix_result_studentresult) 

19.where studentresult between 80 and 90 and student.studentno = result.studentno and subject.subjectid = result.subjectid 

第六章上機練習

begin transaction declare errorsum int set errorsum 0 插入資料 insert into result studentno,subjectno,examdate,studentresult values 1,1,getdate 90 set err...

優化Mychool 第六章上機

begin transaction declare errorsum int set errorsum 0 插入資料 insert into result studentno,subjectno,examdate,studentresult values 1,1,getdate 90 set err...

mysql第六章 第六章 mysql日誌

第六章 mysql日誌 一 錯誤日誌 錯誤日誌的預設存放路徑是 mysql 存放資料的地方 hostname.err 1.修改錯誤日誌存放路徑 mysqld log error data mysql mysql.log 2.檢視配置命令 show variables like log error 3...