实验八T-SQL的使用

实验六   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'

数据库实验就此结束,就最后的课程设计了 。

 

 

 

 

 

 

你可能感兴趣的:(数据库,数据库实验)