存储过程,事务,触发器等详解_第1页
存储过程,事务,触发器等详解_第2页
存储过程,事务,触发器等详解_第3页
存储过程,事务,触发器等详解_第4页
存储过程,事务,触发器等详解_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、存储过程:        预先用SQL语句写好的,并用存储起来,如果需要的数据库提供与定义好的存储过程的功能相同时,只要调用execute()方法,即可执行。触发器:        个人认为是一种特殊的存储过程,因为它是当运行到标签所在的位置时,才触发这个SQL语名的功能.事务:       事务就是一个单元的工作,包括一系列的操作,这些操作要么全部成功,要么全部失败。锁:  

2、     锁就是保护指定的资源,不被其他事务操作。事务和锁的特点 事务的特点: 1. 事务是一个单元的工作,要么全做,要么全不做 2. 事务保证操作的一致性和可恢复性 3. 每一条Transact-SQL语句都可以是一个事务 4. 实际使用的事务是用户定义的事务,它包括一系列操作或者语句 5. 在多服务器环境中,使用用户定义的分布式事务,保证操作的一致性 锁的特点: 1. 锁是保证并发控制的手段 2. 可以锁定的资源包括行、页、簇、表和数据库 3. 锁的类型主要包括共享锁和排它锁 4. 特殊类型的锁包括意图锁、修改锁和模式锁 5. 共享锁允许其他事务继续

3、使用锁定的资源 6. 排它锁只允许一个事务访问数据 7. 系统本身可以处理死锁 8. 用户可以根据实际情况定制锁的一些特征=-创建数据库 scroll dynamiccreate database StudentDBGO-置此数据库为当前数据库use StudentDBGO-创建学生表create table student(    SId varchar(20) primary key,     

4、0;          -学生编号    SName varchar(20),                          -学生姓名    &

5、#160;  SClass varchar(20),                         -学生班级        SSex varchar(10),     

6、0;                     -学生性别    SScore float default(0) check(SScore>=0)    -学生平均分    )GO-创建课程表create table 

7、;class(    EId varchar(20) primary key,                -课程编号    EName varchar(20),             

8、             -课程名称     ETime int check(ETime>=0)                   -课程课时)GO-创建分数表create table

9、0;score(    SId varchar(20),             -学生编号     EId varchar(20),             -课程编号     

10、EScore float,                -课程分数       primary key(SID,EID),        -定义主码     foreign key (SID)

11、60;references student(SID) on delete cascade, -声明及联删除    foreign key (EID) references class(EID) on delete cascade    -声明及联删除)GO-创建计算平均值得触发器create trigger trigger_avg_insert on sco

12、re for insertasbegin transactiondeclare count intupdate student set SScore=(select avg(EScore) from score where SId=(select SId from inserted) where SId=(select SId from inserted)select coun

13、t=errorif(count=0)    commit transactionelse    rollback transaction-创建计算平均值得触发器create trigger trigger_avg_delete on score for deleteasbegin transactionupdate student set SScore=(select avg(EScor

14、e) from score where SId=(select SId from deleted) where SId=(select SId from deleted)declare count intselect count=errorif(count=0)    commit transactionelse    rollback trans

15、action-创建计算平均值得触发器create trigger trigger_avg_update on score for updateasbegin transactiondeclare count intupdate student set SScore=(select avg(EScore) from score where SId=(select SId from inserted)

16、 where SId=(select SId from deleted)select count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建查询学生信息的存储过程create proc proc_student_selectasbegin transactiondeclare count 

17、intselect * from studentselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建查找平均分存储过程CREATE PROCEDURE proc_student_avg(    SID varchar(20)ASbegin transactions

18、elect avg(EScore) as SAvg from score where SId=SIddeclare count intselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建通过学号查询学生信息的存储过程create proc proc_student

19、_select_bySId(    SId varchar(20)asbegin transactiondeclare count intselect * from student where SId=SIdselect count=errorif(count=0)    commit transactionelse    rollback transact

20、ionGO-创建插入学生信息的存储过程create proc proc_student_insert(    SId varchar(20),    SName varchar(20),    SClass varchar(20),    SSex varchar(10)asbegin transactiondeclare count intins

21、ert into student(SID,SName,SClass,SSex) values(SId,SName,SClass,SSex)select count=errorif(count=0)    commit transactionelse    rollback transactionGO-删除学生信息的存储过程create proc proc_student_delete(    SI

22、d varchar(20)asbegin transactiondeclare count intdelete from student where SId=SIdselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-修改学生信息的存储过程create proc proc_stu

23、dent_update(    SId varchar(20),    SName varchar(20),    SClass varchar(20),    SSex varchar(10)asbegin transactiondeclare count intupdate student set SName=SName,SClass

24、=SClass,SSex=SSex where SId=SIdselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建查询课程信息的存储过程create proc proc_class_selectasbegin transactiondeclare count intselect * 

25、from classselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建通过课程号查询课程信息的存储过程create proc proc_class_select_byEId(        EId varchar(20)asbegin tra

26、nsactiondeclare count intselect * from class where EId=EIdselect count=errorif(count=0)    commit transactionelse    rollback transaction-创建插入课程信息的存储过程GOcreate proc proc_class_insert(   

27、; EId varchar(20),    EName varchar(20),    ETime int)asbegin transactiondeclare count intinsert into class(EId,EName,ETime) values(EId,EName,ETime)select count=errorif(count=0)    comm

28、it transactionelse    rollback transaction-创建删除课程信息的存错过程GOcreate proc proc_class_delete(    EId varchar(20)asbegin transactiondeclare count intdelete from class where EId=EIdselect count=errorif(

29、count=0)    commit transactionelse    rollback transaction-创建修改课程信息的存储过程GOcreate proc proc_class_update(    EId varchar(20),    EName varchar(20),    ETime int)asbeg

30、in transactiondeclare count intupdate class set EName=EName,ETime=ETime where EId=EIdselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建查询成绩信息的存储过程create proc pro

31、c_score_selectasbegin transactiondeclare count intselect * from scoreselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建通过学号查询成绩信息的存储过程create proc proc_score_select_bySId(&

32、#160;   SId varchar(20)asbegin transactiondeclare count intselect * from score where SId=SIdselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建通过查询成绩信息的

33、存储过程create proc proc_score_select_byEId(    EId varchar(20)asbegin transactiondeclare count intselect * from score where EId=EIdselect count=errorif(count=0)    commit transactionelse  

34、  rollback transactionGO-创建插入成绩信息的存储过程create proc proc_score_insert(    SId varchar(20),    EId varchar(20),    EScore float)asbegin transactiondeclare count int    

35、insert into score(SId,EId,EScore) values(SId,EId,EScore)    select count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建删除成绩信息的存错过程create proc proc_score_delete(  

36、60; SId varchar(20),    EId varchar(20)asbegin transactiondeclare count intdelete from score where SId=SId and EId=EIdselect count=errorif(count=0)    commit transactionelse   

37、; rollback transactionGO-创建通过学号删除成绩信息的存错过程create proc proc_score_delete_bySId(    SId varchar(20)asbegin transactiondeclare count intdelete from score where SId=SIdselect count=errorif(count=0)   

38、0;commit transactionelse    rollback transactionGO-创建通过课程号删除成绩信息的存错过程create proc proc_score_delete_byEId(    EId varchar(20)asbegin transactiondeclare count intdelete from score where EId=EIdselect

39、60;count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建修改成绩信息的存储过程create proc proc_score_update(    SId varchar(20),    EId varchar(20),    EScor

40、e float)asbegin transactiondeclare count intupdate score set EScore=EScore where SId=SId and EId=EIdselect count=errorif(count=0)    commit transactionelse    rollback transactionGO-创建查询学生所有

41、信息的存储过程create proc proc_student_one_information(    SId varchar(20)asbegin transactiondeclare count intselect student.SName,student.SClass,student.SSex,class.EName,class.ETime,score.EScore,student.SScore from student,class,score where student.SId=score.SId and class.EId=score.EId and student.SId=SIdselect count=errorif(count=0)   

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论