From 4926c2567861194b22d7e602c4887225e7984100 Mon Sep 17 00:00:00 2001 From: lzlk <1930069369@qq.com> Date: Thu, 29 Sep 2022 00:30:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-28\344\275\234\344\270\232.sql" | 48 +++++++++++++++++++ ...4\350\256\260 \344\272\213\345\212\241.md" | 21 ++++++++ 2 files changed, 69 insertions(+) create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-28\344\275\234\344\270\232.sql" create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-28\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-28\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..ad54ab8 --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-28\344\275\234\344\270\232.sql" @@ -0,0 +1,48 @@ +--假设刘备取款6000,(添加check约束,设置账户余额必须>=0),要求:使用事务实现,修改余额和添加取款记录两步操作使用事务 + use BankTest + alter table bankcard add constraint money check(cardmoney>=0) + update BankCard set CardMoney=7000 where AccountId=1 + begin transaction + declare @myerr int = 0 + declare @money decimal(13,2)=6000, + @id int=( + select AccountId from AccountInfo where RealName='刘备') + declare + @price decimal(13,2)=(select CardMoney from BankCard where + AccountId=@id) + declare @cardno varchar(50)=(select cardno from BankCard where AccountId=@id); + update BankCard set CardMoney-=@money where AccountId=@id; + set @myerr+=@@ERROR + insert into CardExchange values(@cardno,@money,0,getdate()); + set @myerr+=@@ERROR + if (@myerr = 0) + begin + commit transaction + end + else + begin + rollback transaction + end +--刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) + begin transaction + declare @false int=0 + declare @money1 decimal(13,2)=1000, + @outid varchar(50)=(select AccountId from AccountInfo where RealName='刘备'), + @inid varchar(50)=(select AccountId from AccountInfo where RealName='张飞') + declare + @outcard varchar(50)=(select cardno from BankCard where AccountId=@outid ), + @incard varchar(50)=(select cardno from BankCard where AccountId=@inid ) + update BankCard set CardMoney+=@money1 where AccountId=@inid + set @false+=@@ERROR + update BankCard set CardMoney-=@money1 where AccountId=@outid + set @false+=@@ERROR + insert into CardTransfer values(@outcard,@incard,@money1,GETDATE()) + set @false+=@@ERROR + if(@false=0) + begin + commit transaction + end + else + begin + rollback transaction + end \ No newline at end of file diff --git "a/07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md" "b/07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md" new file mode 100644 index 0000000..01eb65c --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md" @@ -0,0 +1,21 @@ +浜嬪姟锛 +浜嬪姟鐨勫睘鎬э細ACID +鍘熷瓙鎬э紙Atomicity锛屾垨绉颁笉鍙垎鍓叉э級銆佷竴鑷存э紙Consistency锛夈侀殧绂绘э紙Isolation锛屽張绉扮嫭绔嬫э級銆佹寔涔呮э紙Durability) + +寮濮嬩簨鍔 begin transaction(鎴栬卋egin tran) +declare @false int=0 +鍦ㄥ鍒犳敼鍚巗et @false+=@error + +缁撴潫鍒ゆ柇 +--浜嬪姟鎻愪氦锛氬鏋滆鍙ラ兘鎵ц鎴愬姛 +if (@false= 0) + begin + commit transaction + end +--浜嬪姟鍥炴粴锛氬鏋滀簨鍔′腑鏈変竴鏉¤鍙ユ墽琛屽け璐ワ紝 rollback tran/transaction +else + begin + rollback transaction + end +check绾︽潫: +alter table 浠庤〃 add constraint check() \ No newline at end of file -- Gitee