實驗六 T_SQL的應用
一、實驗目的:
1.掌握在SQL_SERVER2012觸發器的定義和作用。
2.掌握在SQL_SERVER2012存儲過程的定義和作用。
3.掌握在SQL_SERVER2012的事務定義和作用。
二、實驗準備
複習課堂筆記中相關內容的介紹,設計相關程序。
三、實驗內容
1.驗證如下範例,並記錄結果,分析各個觸發器和存儲過程的作用。
例1:CREATE TRIGGER tri1
ON sc
FOR insert
AS
update student setcnum=cnum+1
wherestudent.sno=(select sno from inserted where student.sno=inserted.sno)
//-----------------------------------------------------------------------------------------------------------------------------//
例2:CREATE TRIGGER tri3
ON sc
FOR update
AS
if update(sno)
begin
update student set cnum=cnum-1
where student.sno=(select sno from deletedwhere student.sno=deleted.sno)
update student set cnum=cnum+1
where student.sno=(select sno from insertedwhere student.sno=inserted.sno)
end
//-----------------------------------------------------------------------------------------------------------------------------//
例3:CREATE PROCEDURECJCXPROC AS
SELECT STUDENT.SNO,SNAME,COURSE.CNO,CNAME,GRADEFROM
STUDENT,COURSE,SC WHERESTUDENT.SNO=SC.SNO AND COURSE.CNO=SC.CNO
GO
//-----------------------------------------------------------------------------------------------------------------------------//
例4:CREATE PROCEDURECJCXPROC1 @x char(5)
AS
selectsc.sno,sname,sc.cno,cname,grade from student,course,sc where student.sno=sc.snoand course.cno=sc.cno and sc.sno=@x order by sc.cno
GO
//-----------------------------------------------------------------------------------------------------------------------------//
例5:ZZZ
end
2.創建一個存儲過程,它的功能是給出一個學生的姓和名,該存儲過程將顯示該學生所學課程名和該課程的分數。定義該過程,並調用運行,觀察結果。
3.在存儲過程中使用事務操作,定義一個存儲過程,該過程作用是向實驗樣例數據庫的Student表中插入一條數據,在插入數據時驗證輸入的學生姓名是否重複。如果不重複,插入數據有效;否則,插入數據無效,回滾事務撤銷操作。
四、實驗報告內容
記錄上機運行情況記錄,如出錯,分析原因並改正,記錄下正確的命令。
code:
CREATE TRIGGER tri1
ON sc
FOR insert
AS
update student set cnum=cnum+1
where student.sno=(select sno from inserted where student.sno=inserted.sno)
CREATE TRIGGER tri3
ON sc
FOR update
AS
if update(sno)
begin
update student set cnum=cnum-1
where student.sno=(select sno from deleted where student.sno=deleted.sno)
update student set cnum=cnum+1
where student.sno=(select sno from inserted where student.sno=inserted.sno)
end
CREATE PROCEDURE CJCXPROC AS
SELECT STUDENT.SNO,SNAME,COURSE.CNO,CNAME,GRADE FROM
STUDENT,COURSE,SC WHERE STUDENT.SNO=SC.SNO AND COURSE.CNO=SC.CNO
GO
CREATE PROCEDURE CJCXPROC2 @x char(5)
AS
select sc.sno,sname,sc.cno,cname,grade from student,course,sc where student.sno=sc.sno and course.cno=sc.cno and sc.sno=@x order by sc.cno
GO
begin transaction t11 with mark 'aaa'
declare @x int
set @x=1
delete from student where sno='200215122'
if @x=-1
begin
rollback transaction t11
end
else
begin
delete from sc where sno='200215122'
commit transaction t11
END
alter PROCEDURE PROC5
as
DECLARE @y CHAR(10)
set @y = '王敏';
select student.sno,sc.cno,grade
from sc,student
where sname = @y
return
go
exec proc5
create procedure proc6
@x1 char(9),@x2 char(20),@x3 char(2),@x4 int,@x5 char(20)
as
begin transaction t12 with mark 'aba'
insert into student values( @x1,@x2,@x3,@x4,@x5)
select * from student
DECLARE @x CHAR(10)
select @x = count(sname) from student
where sname =@x2
if @x>1
begin
rollback transaction t12
select * from student
end
else
begin
commit transaction t12
select * from student
End
go
exec proc6 '200215131','李勇22','男',20,'cs'
數據庫實驗就此結束,就最後的課程設計了 。