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
end
go
[sql] view plain copy 在code上檢視**片派生到我的**片
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
end
go
[sql] view plain copy 在code上檢視**片派生到我的**片
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
go
select * from vw_student_result_info
[sql] view plain copy 在code上檢視**片派生到我的**片
use myschool
go
if 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
第六章上機練習
練習1 批量插入學生考試成績 begin transaction declare errorsum int set errorsum 0 插入資料 insert into result studentno,subjectno,examdate,studentresult values 1,1,get...
優化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...