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

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

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

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

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

3、e student(SId varchar(20) primary key, -学生编号SName varchar(20), -学生姓名 SClass varchar(20), -学生班级 SSex varchar(10), -学生性别SScore float default(0) check(SScore=0) -学生平均分 )GO-创建课程表create table class(EId varchar(20) primary key, -课程编号EName varchar(20), -课程名称 ETime int check(ETime=0) -课程课时)GO-创建分数表create ta

4、ble score(SId varchar(20), -学生编号 EId varchar(20), -课程编号 EScore float, -课程分数 primary key(SID,EID), -定义主码 foreign key (SID) references student(SID) on delete cascade, -声明及联删除foreign key (EID) references class(EID) on delete cascade -声明及联删除)GO-创建计算平均值得触发器create trigger trigger_avg_insert on score for i

5、nsertasbegin transactiondeclare count intupdate student set SScore=(select avg(EScore) from score where SId=(select SId from inserted) where SId=(select SId from inserted)select count=errorif(count=0)commit transactionelserollback transaction-创建计算平均值得触发器create trigger trigger_avg_delete on score for

6、 deleteasbegin transactionupdate student set SScore=(select avg(EScore) from score where SId=(select SId from deleted) where SId=(select SId from deleted)declare count intselect count=errorif(count=0)commit transactionelserollback transaction-创建计算平均值得触发器create trigger trigger_avg_update on score for

7、 updateasbegin transactiondeclare count intupdate student set SScore=(select avg(EScore) from score where SId=(select SId from inserted) where SId=(select SId from deleted)select count=errorif(count=0)commit transactionelserollback transactionGO-创建查询学生信息的存储过程create proc proc_student_selectasbegin tr

8、ansactiondeclare count intselect * from studentselect count=errorif(count=0)commit transactionelserollback transactionGO-创建查找平均分存储过程CREATE PROCEDURE proc_student_avg(SID varchar(20)ASbegin transactionselect avg(EScore) as SAvg from score where SId=SIddeclare count intselect count=errorif(count=0)com

9、mit transactionelserollback transactionGO-创建通过学号查询学生信息的存储过程create proc proc_student_select_bySId(SId varchar(20)asbegin transactiondeclare count intselect * from student where SId=SIdselect count=errorif(count=0)commit transactionelserollback transactionGO-创建插入学生信息的存储过程create proc proc_student_inser

10、t(SId varchar(20),SName varchar(20),SClass varchar(20),SSex varchar(10)asbegin transactiondeclare count intinsert into student(SID,SName,SClass,SSex) values(SId,SName,SClass,SSex)select count=errorif(count=0)commit transactionelserollback transactionGO-删除学生信息的存储过程create proc proc_student_delete(SId

11、varchar(20)asbegin transactiondeclare count intdelete from student where SId=SIdselect count=errorif(count=0)commit transactionelserollback transactionGO-修改学生信息的存储过程create proc proc_student_update(SId varchar(20),SName varchar(20),SClass varchar(20),SSex varchar(10)asbegin transactiondeclare count i

12、ntupdate student set SName=SName,SClass=SClass,SSex=SSex where SId=SIdselect count=errorif(count=0)commit transactionelserollback transactionGO-创建查询课程信息的存储过程create proc proc_class_selectasbegin transactiondeclare count intselect * from classselect count=errorif(count=0)commit transactionelserollback

13、 transactionGO-创建通过课程号查询课程信息的存储过程create proc proc_class_select_byEId( EId varchar(20)asbegin transactiondeclare count intselect * from class where EId=EIdselect count=errorif(count=0)commit transactionelserollback transaction-创建插入课程信息的存储过程GOcreate proc proc_class_insert(EId varchar(20),EName varchar

14、(20),ETime int)asbegin transactiondeclare count intinsert into class(EId,EName,ETime) values(EId,EName,ETime)select count=errorif(count=0)commit transactionelserollback transaction-创建删除课程信息的存错过程GOcreate proc proc_class_delete(EId varchar(20)asbegin transactiondeclare count intdelete from class where

15、 EId=EIdselect count=errorif(count=0)commit transactionelserollback transaction-创建修改课程信息的存储过程GOcreate proc proc_class_update(EId varchar(20),EName varchar(20),ETime int)asbegin transactiondeclare count intupdate class set EName=EName,ETime=ETime where EId=EIdselect count=errorif(count=0)commit transactionelserollback transactionGO-创建查询成绩信息的存储过程create proc proc_score_selectasbeg

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

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