diff --git "a/35\351\273\204\346\237\261\350\217\230/\351\273\204\346\237\261\350\217\23035/2022-9-28.sql" "b/35\351\273\204\346\237\261\350\217\230/\351\273\204\346\237\261\350\217\23035/2022-9-28.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cea0107133219c2b459474b07c0d5c91fda3992e --- /dev/null +++ "b/35\351\273\204\346\237\261\350\217\230/\351\273\204\346\237\261\350\217\23035/2022-9-28.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/35\351\273\204\346\237\261\350\217\230/\351\273\204\346\237\261\350\217\23035/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md.url" "b/35\351\273\204\346\237\261\350\217\230/\351\273\204\346\237\261\350\217\23035/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md.url" new file mode 100644 index 0000000000000000000000000000000000000000..9f4054e771c42132f620f3ca7bebc05c60e5e0f4 --- /dev/null +++ "b/35\351\273\204\346\237\261\350\217\230/\351\273\204\346\237\261\350\217\23035/2022-9-28\347\254\224\350\256\260 \344\272\213\345\212\241.md.url" @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://gitee.com/level-21-software-class-4/advanced-sql/blob/master/07%20%E5%BB%96%E6%B2%BB%E5%85%88/%E7%AC%94%E8%AE%B0/2022-9-28%E7%AC%94%E8%AE%B0%20%E4%BA%8B%E5%8A%A1.md