存储过程,事务,触发器等详解.doc

上传人:夏** 文档编号:559392869 上传时间:2023-12-16 格式:DOC 页数:16 大小:62.50KB
返回 下载 相关 举报
存储过程,事务,触发器等详解.doc_第1页
第1页 / 共16页
存储过程,事务,触发器等详解.doc_第2页
第2页 / 共16页
存储过程,事务,触发器等详解.doc_第3页
第3页 / 共16页
存储过程,事务,触发器等详解.doc_第4页
第4页 / 共16页
存储过程,事务,触发器等详解.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《存储过程,事务,触发器等详解.doc》由会员分享,可在线阅读,更多相关《存储过程,事务,触发器等详解.doc(16页珍藏版)》请在金锄头文库上搜索。

1、存储过程: 预先用SQL语句写好的,并用存储起来,如果需要的数据库提供与定义好的存储过程的功能相同时,只要调用execute()方法,即可执行。触发器: 个人认为是一种特殊的存储过程,因为它是当运行到标签所在的位置时,才触发这个SQL语名的功能.事务: 事务就是一个单元的工作,包括一系列的操作,这些操作要么全部成功,要么全部失败。锁: 锁就是保护指定的资源,不被其他事务操作。事务和锁的特点 事务的特点: 1. 事务是一个单元的工作,要么全做,要么全不做 2. 事务保证操作的一致性和可恢复性 3. 每一条Transact-SQL语句都可以是一个事务 4. 实际使用的事务是用户定义的事务,它包括一

2、系列操作或者语句 5. 在多服务器环境中,使用用户定义的分布式事务,保证操作的一致性 锁的特点: 1. 锁是保证并发控制的手段 2. 可以锁定的资源包括行、页、簇、表和数据库 3. 锁的类型主要包括共享锁和排它锁 4. 特殊类型的锁包括意图锁、修改锁和模式锁 5. 共享锁允许其他事务继续使用锁定的资源 6. 排它锁只允许一个事务访问数据 7. 系统本身可以处理死锁 8. 用户可以根据实际情况定制锁的一些特征=-创建数据库scrolldynamiccreatedatabaseStudentDBGO-置此数据库为当前数据库useStudentDBGO-创建学生表createtablestudent

3、(SIdvarchar(20)primarykey,-学生编号SNamevarchar(20),-学生姓名SClassvarchar(20),-学生班级SSexvarchar(10),-学生性别SScorefloatdefault(0)check(SScore=0)-学生平均分)GO-创建课程表createtableclass(EIdvarchar(20)primarykey,-课程编号ENamevarchar(20),-课程名称ETimeintcheck(ETime=0)-课程课时)GO-创建分数表createtablescore(SIdvarchar(20),-学生编号EIdvarchar

4、(20),-课程编号EScorefloat,-课程分数primarykey(SID,EID),-定义主码foreignkey(SID)referencesstudent(SID)ondeletecascade,-声明及联删除foreignkey(EID)referencesclass(EID)ondeletecascade-声明及联删除)GO-创建计算平均值得触发器createtriggertrigger_avg_insertonscoreforinsertasbegintransactiondeclarecountintupdatestudentsetSScore=(selectavg(ES

5、core)fromscorewhereSId=(selectSIdfrominserted)whereSId=(selectSIdfrominserted)selectcount=errorif(count=0)committransactionelserollbacktransaction-创建计算平均值得触发器createtriggertrigger_avg_deleteonscorefordeleteasbegintransactionupdatestudentsetSScore=(selectavg(EScore)fromscorewhereSId=(selectSIdfromdele

6、ted)whereSId=(selectSIdfromdeleted)declarecountintselectcount=errorif(count=0)committransactionelserollbacktransaction-创建计算平均值得触发器createtriggertrigger_avg_updateonscoreforupdateasbegintransactiondeclarecountintupdatestudentsetSScore=(selectavg(EScore)fromscorewhereSId=(selectSIdfrominserted)whereSId

7、=(selectSIdfromdeleted)selectcount=errorif(count=0)committransactionelserollbacktransactionGO-创建查询学生信息的存储过程createprocproc_student_selectasbegintransactiondeclarecountintselect*fromstudentselectcount=errorif(count=0)committransactionelserollbacktransactionGO-创建查找平均分存储过程CREATEPROCEDUREproc_student_avg

8、(SIDvarchar(20)ASbegintransactionselectavg(EScore)asSAvgfromscorewhereSId=SIddeclarecountintselectcount=errorif(count=0)committransactionelserollbacktransactionGO-创建通过学号查询学生信息的存储过程createprocproc_student_select_bySId(SIdvarchar(20)asbegintransactiondeclarecountintselect*fromstudentwhereSId=SIdselectc

9、ount=errorif(count=0)committransactionelserollbacktransactionGO-创建插入学生信息的存储过程createprocproc_student_insert(SIdvarchar(20),SNamevarchar(20),SClassvarchar(20),SSexvarchar(10)asbegintransactiondeclarecountintinsertintostudent(SID,SName,SClass,SSex)values(SId,SName,SClass,SSex)selectcount=errorif(count=

10、0)committransactionelserollbacktransactionGO-删除学生信息的存储过程createprocproc_student_delete(SIdvarchar(20)asbegintransactiondeclarecountintdeletefromstudentwhereSId=SIdselectcount=errorif(count=0)committransactionelserollbacktransactionGO-修改学生信息的存储过程createprocproc_student_update(SIdvarchar(20),SNamevarcha

11、r(20),SClassvarchar(20),SSexvarchar(10)asbegintransactiondeclarecountintupdatestudentsetSName=SName,SClass=SClass,SSex=SSexwhereSId=SIdselectcount=errorif(count=0)committransactionelserollbacktransactionGO-创建查询课程信息的存储过程createprocproc_class_selectasbegintransactiondeclarecountintselect*fromclassselec

12、tcount=errorif(count=0)committransactionelserollbacktransactionGO-创建通过课程号查询课程信息的存储过程createprocproc_class_select_byEId(EIdvarchar(20)asbegintransactiondeclarecountintselect*fromclasswhereEId=EIdselectcount=errorif(count=0)committransactionelserollbacktransaction-创建插入课程信息的存储过程GOcreateprocproc_class_insert(EIdvarchar(20),ENamevarchar(20),ETimeint)asbegintrans

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号