『SQLServer系列教程』——事务详解

请添加图片描述

请添加图片描述
📣读完这篇文章里你能收获到

  • 了解SQLServer事务的特性及分类
  • 事务的创建过程
  • 提供实际操作的案例SQL脚本

请添加图片描述

请添加图片描述

一、概念篇

1.什么是事务

在这里插入图片描述

2.事务的特性

在这里插入图片描述

3.事务的分类

在这里插入图片描述

请添加图片描述

二、事务的创建

在这里插入图片描述
在这里插入图片描述

请添加图片描述

三、案例代码

示例1:使用事务回滚有错误的数据

use StudentManager
go
declare @errorSum int --定义变量,用于累计事务执行过程中的错误
set @errorSum=0        --初始化为0,即无错误
begin transaction
   begin		
		update CardAccount set CurrentMoney=CurrentMoney-1000
					where StudentId=100001
       set @errorSum=@errorSum+@@error --累计是否有错误		
		update CardAccount set CurrentMoney=CurrentMoney+1000
					where StudentId=100002
      set @errorSum=@errorSum+@@error --累计是否有错误
      if(@errorSum>0)
			rollback transaction
      else 
          commit transaction
 end
go
--查询余额
select Students.StudentId,StudentName,CurrentMoney from Students
inner join CardAccount on Students.StudentId=CardAccount.StudentId

示例2:启用事务的存储过程

use StudentManager
go
if exists(select * from sysobjects where name='usp_TransferAccounts')
drop procedure usp_TransferAccounts
go
create procedure usp_TransferAccounts
@inputAccount int,--转入账户
@outputAccount int,--转出账户
@transferMoney int --交易金额
as
	declare @errorSum int 
	set @errorSum=0      
	begin transaction
	   begin		
			update CardAccount set CurrentMoney=CurrentMoney-@transferMoney
						where StudentId=@outputAccount
			set @errorSum=@errorSum+@@error 	
			update CardAccount set CurrentMoney=CurrentMoney+@transferMoney
						where StudentId=@inputAccount
		   set @errorSum=@errorSum+@@error 
		   if(@errorSum>0)
				rollback transaction
		   else 
				commit transaction
	 end	
go
--测试失败的转账
exec usp_TransferAccounts 100002,100001,1000
--查询余额
select Students.StudentId,StudentName,CurrentMoney from Students
inner join CardAccount on Students.StudentId=CardAccount.StudentId
--测试成功的转账
exec usp_TransferAccounts 100002,100001,800
--查询余额
select Students.StudentId,StudentName,CurrentMoney from Students
inner join CardAccount on Students.StudentId=CardAccount.StudentId

请添加图片描述

请添加图片描述

SQL Server DBA教程是为学习SQL Server数据库管理技能的学生和专业人士设计的教学材料。该教程旨在帮助学习者掌握SQL Server数据库管理的各个方面,包括设置、配置、监控、维护和优化数据库。 首先,SQL Server DBA教程将介绍SQL Server数据库的基本概念和架构。学习者将了解SQL Server数据库组成的各个组件,如实例、数据库、表和索引,以及它们之间的关系。 然后,教程将重点介绍SQL Server数据库服务器的设置和配置。学习者将学会如何安装和配置SQL Server实例,设置数据库的参数和选项,以及创建和管理数据库账户。 接下来,教程将教授学习者如何监控SQL Server数据库的性能和健康状况。学习者将学习如何使用系统监视器和动态管理视图来跟踪数据库活动和性能指标。他们还将学会使用SQL Server代理来创建和计划作业,以及使用警报和通知来监控数据库的异常情况。 此外,SQL Server DBA教程还将涵盖数据库备份和恢复的概念和技术。学习者将学会如何创建定期备份,并使用SQL Server的恢复模型来恢复数据库。他们还将了解如何执行点恢复和完整恢复,以及如何使用事务日志来恢复数据库。 最后,教程将介绍SQL Server数据库的维护和性能优化。学习者将学习如何执行数据库维护任务,如索引重建和统计更新。他们还将了解如何使用索引和查询优化技术来提高数据库的性能和响应速度。 总结起来,SQL Server DBA教程提供了一个全面的学习路径,帮助学习者成为熟练的SQL Server数据库管理者。通过理论和实践的结合,学习者将获得必要的知识和技能,以有效地管理和优化SQL Server数据库。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老陈聊架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值