From 808f03c9c65feb5b0aad24c64ff6a01adc72cad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Thu, 29 Sep 2022 04:11:22 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-09-29=EF=BC=88?= =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/.keep" diff --git "a/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 3a99852601b97503627071bf5ae32f251bdb6c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Thu, 29 Sep 2022 04:11:55 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庢稕 <1986456126@qq.com> --- .../SQLQuery1.sql" | 70 +++++++++ .../\347\254\224\350\256\260.txt" | 0 ...0\347\254\224\350\256\260\357\274\211.sql" | 135 ++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 "09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" create mode 100644 "09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" create mode 100644 "09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213\357\274\210\347\254\224\350\256\260\357\274\211.sql" diff --git "a/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" "b/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" new file mode 100644 index 0000000..14b5183 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" @@ -0,0 +1,70 @@ +--事务练习(BankTest): +select * from BankCard +select * from AccountInfo + +--1.假设刘备取款6000,(添加check约束,设置账户余额必须>=0),要求:使用事务实现,修改余额和添加取款记录两步操作使用事务 + +alter table BankCard add constraint CK_Money check(CardMoney >= 0) + +declare @myError int = 0 +declare @getMoney float = 6000 +declare @carID varchar(10) +declare @name varchar(10) +declare @Phone varchar(10) +set @name = '刘备' + + +select @carID = (select b.CardNo from AccountInfo a inner join BankCard b on a.AccountId =b.AccountId where RealName = @name) +select @Phone = (select a.AccountPhone from AccountInfo a inner join BankCard b on a.AccountId =b.AccountId where RealName = @name) +--开始事务 +begin transaction A +update BankCard set CardMoney -= @getMoney where @carID=CardNo +set @myError += @@ERROR +insert into CardExchange values(@carID,0,@getMoney,getdate()) +set @myError += @@ERROR +--事务提交 +if(@myError = 0) +begin + commit transaction A +end +else +begin + rollback transaction B +end +go +--2.刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) + +alter table BankCard add constraint CK_Money check(CardMoney >= 0) +declare @myError int = 0 +declare @getMoney float = 1000 +declare @carID varchar(10) +declare @carID1 varchar(10) +declare @name varchar(10) +declare @name1 varchar(10) +declare @Phone varchar(10) +declare @Phone1 varchar(10) +set @name = '刘备' +set @name1 = '张飞' +select @carID = (select b.CardNo from AccountInfo a inner join BankCard b on a.AccountId =b.AccountId where RealName = @name) +select @Phone = (select a.AccountPhone from AccountInfo a inner join BankCard b on a.AccountId =b.AccountId where RealName = @name) +select @carID1 = (select b.CardNo from AccountInfo a inner join BankCard b on a.AccountId =b.AccountId where RealName = @name1) +select @Phone1 = (select a.AccountPhone from AccountInfo a inner join BankCard b on a.AccountId =b.AccountId where RealName = @name1) + +begin transaction +update BankCard set CardMoney -= @getMoney where CardNo = @carID +set @myError += @@ERROR +update BankCard set CardMoney += @getMoney where CardNo = @carID1 +set @myError += @@ERROR +insert into CardTransfer values(@carID,@carID1,@getMoney,getdate()) +set @myError += @@ERROR + +if(@myError = 0) +begin + commit transaction +end +else +begin + rollback transaction +end + + diff --git "a/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" "b/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..e69de29 diff --git "a/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213\357\274\210\347\254\224\350\256\260\357\274\211.sql" "b/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213\357\274\210\347\254\224\350\256\260\357\274\211.sql" new file mode 100644 index 0000000..a63a561 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-09-29\357\274\210\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213\357\274\210\347\254\224\350\256\260\357\274\211.sql" @@ -0,0 +1,135 @@ +--产品信息 +create table Products( + prodID int primary key identity(1,1), --产品编号 + prodName varchar(50), --产品名称 + Qty int, --库存 + price money, --成本价 +) + +--购买者信息 +create table Purchase( + purchID int primary key identity(1,1), + PurchPerson varchar(20), --下单人 + purchMoney money --余额 + +) + +--订单详情表 +create table Orders( + orderID int primary key identity(1,1), + purchID int references Purchase(purchID), + productID int references Products(prodID), + orderDate datetime default(getdate()), + orderPMoney money --总金额 +) +go + +--余额>=0 增加check约束 +alter table Purchase add constraint CK_Money check(purchMoney >= 0) +--库存>=0 +alter table Products add constraint CK_Qty check(Qty >= 0) + + +insert into Products values('咖啡',200,5.00) +insert into Products values('茶叶',100,100.00) +insert into Products values('牛奶',500,60.00) + +select * from Products + +select * from Purchase +insert into Purchase values('蜗牛',3000) + + +--方法1 +--使用@@error判断 + +--事务 +--开始事务 begin transaction , begin tran +begin transaction A +declare @num int = 3 +declare @goods varchar(10) = '茶叶' +--单价 +declare @price money = (select price from Products where prodName=@goods) +--购买物品ID +declare @proId int = (select prodID from Products where prodName=@goods) +--购买者 +declare @purchId int = (select purchID from Purchase where PurchPerson = '蜗牛') + +--错误代码 +declare @myerr int = 0 + +--1.库存是否充足 +update Products set Qty-= @num where prodName=@goods + set @myerr += @@error + save tran B +--2.余额减少 +update Purchase set purchMoney -= (@num*@price) where PurchPerson = '蜗牛' + set @myerr += @@error +--3.生成订单信息 +insert into Orders values(@purchId,@proId,getdate(),(@num*@price)) + + +--事务提交:如果语句都执行成功, commit tran/transaction +if (@myerr = 0) + begin + commit transaction A + end +--事务回滚:如果事务中有一条语句执行失败, rollback tran/transaction +else + begin + rollback transaction B + end + + +select * from Purchase +select * from Products +select * from Orders + + + +--select * from Products +--print @@error --错误码:0:执行成功 非0的时候代表该语句有错误 + +--标记事务回滚起点 : save tran B + +--方法2: +----begin try +---- --事务的开始:begin tran +---- --sql语句 +---- commit --提交 +----end try + +----begin catch --捕获错误,如果某条语句执行错误,将被捕获 +---- rollback tran +----end + + + +begin try + begin tran + declare @num int = 2 + declare @goods varchar(10) = '茶叶' + --单价 + declare @price money = (select price from Products where prodName=@goods) + --购买物品ID + declare @proId int = (select prodID from Products where prodName=@goods) + --购买者 + declare @purchId int = (select purchID from Purchase where PurchPerson = '蜗牛') + --错误代码 + declare @myerr int = 0 + --1.库存是否充足 + update Products set Qty-= @num where prodName=@goods + set @myerr += @@error + --save tran B + --2.余额减少 + update Purchase set purchMoney -= (@num*@price) where PurchPerson = '蜗牛' + set @myerr += @@error + --3.生成订单信息 + insert into Orders values(@purchId,@proId,getdate(),(@num*@price)) + commit + +end try + +begin catch + rollback tran +end catch \ No newline at end of file -- Gitee