練習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
as select 姓名=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:使用索引查詢學生考試成績
use myschool
goif exists (select name from sysindexes
where name = 'ix_result_studentresult')
drop index result.ix_result_studentresult --刪除索引
go--考試成績列上建立可重複索引:填充因子為70%--
create index ix_result_studentresult
on result(studentresult)
with fillfactor = 70
go--查詢輸出學生姓名、課程名稱、考試日期和考試成績
select studentname '學生姓名',subjectname '課程名稱',examdate '考試日期', studentresult '考試成績' from subject,student,result
with (index = ix_result_studentresult)
where studentresult between 80 and 90 and student.studentno = result.studentno and subject.subjectid = result.subjectid
資料庫第六章
一 關係模式由五部分組成,是乙個五元組 r u,d,dom,f 關係名r是符號化的元組語義 u為一組屬性 d為屬性組u中的屬性所來自的域 dom為屬性到域的對映 f為屬性組u上的一組資料依賴 二 一對一聯絡 設x,y為關係中的屬性或屬性組,它們的所有可能取值組成兩個集合。如果對於x中的任一具體值y中...
資料庫第六章
1.一對一聯絡 2.一對多聯絡 3.多對多聯絡 是乙個關係內部屬性與屬性之間的一種約束關係 是現實世界屬性間相互聯絡的抽象 是資料內在的性質 是語義的體現 1.函式依賴 2.多值依賴 例 描述乙個學生關係,可以有學號 姓名 系名等屬性。乙個學號只對應乙個學生,乙個學生只在乙個系中學習 學號 值確定後...
第六章上機練習
練習1 批量插入學生考試成績 begin transaction declare errorsum int set errorsum 0 插入資料 insert into result studentno,subjectno,examdate,studentresult values 1,1,get...