From 57122e571c601e6b215032c3832d75c31f67b17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E6=9F=8F=E6=88=90?= <2771940338@qq.com> Date: Thu, 29 Sep 2022 11:53:53 +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\347\254\224\350\256\260.txt" | 73 ++++++++++++ .../SQLQuery2022-9-29.sql" | 106 ++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 "08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/2022-9-28\347\254\224\350\256\260.txt" create mode 100644 "08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery2022-9-29.sql" diff --git "a/08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/2022-9-28\347\254\224\350\256\260.txt" "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/2022-9-28\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..af0b057 --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/2022-9-28\347\254\224\350\256\260.txt" @@ -0,0 +1,73 @@ +事务 + + + + + 什么是事务 + +事务( Transaction)由一次或者多次基本操作构成,或者说,事务由一条或者多条 SQL 语句构成。 + +事务中的所有 SQL 语句是一个整体,共同进退,不可分割,要么全部执行成功,要么全部执行失败。 + + + + + + + + 事务的属性(ACID) + +一般来说,事务具有四个标准属性 分别是 + 原子性(*A*tomicity,或称不可分割性 + 一致性(**C**onsistency) + 隔离性(**I**solation,又称独立性 + 持久性(**D**urability),简称 *ACID* + +具体说明如下: + 1) 原子性 + +一个事务中的所有 SQL 语句,要么全部执行成功, +要么全部执行失败,不会结束在中间的某个环节。 +事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态, +就像这个事务从来没有执行过一样。 + + 2) 一致性 + +在事务开始之前和事务结束以后,数据库的完整性没有被破坏。 +这表示写入的数据必须完全符合所有的预设规则, +其中包含数据的精确度、串联性以及后续数据库可以自发性地完成预定的工作。 + + 3) 隔离性 + +数据库允许多个并发事务同时对其数据进行读写和修改的能力, +隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。 +事务隔离分为不同级别,包括读未提交(Read uncommitted)、 +读提交(read committed)、可重复读(repeatable read)和串行化(Serializable)。 + 死锁 操作系统 + + 4) 持久性 + +事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。 + + + + + + + + 事务分类 + + 自动提交事物: + 是SQL Server默认的一种事务模式,每条Sql语句都被看成一个事务进行处理。 + + 显式事务: +T-sql标明,由Begin Transaction开启事务开始,由Commit Transaction 提交事务、Rollback Transaction 回滚事务结束。 + + 隐式事务: +使用Set IMPLICIT_TRANSACTIONS ON 将将隐式事务模式打开,不用Begin Transaction开启事务,当一个事务结束,这个模式会自动启用下一个事务,只用Commit Transaction 提交事务、Rollback Transaction 回滚事务即可。 + +TIPS: + 事物回滚: + +xact_abort on/off : 为on时如果当前sql出错,回滚整个事务, +为off时如果sql出错回滚当前sql语句,其它语句照常运行读写数据库。 \ No newline at end of file diff --git "a/08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery2022-9-29.sql" "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery2022-9-29.sql" new file mode 100644 index 0000000..636e04c --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery2022-9-29.sql" @@ -0,0 +1,106 @@ +--ת +CREATE TABLE grades1 ( + id int NOT NULL , + name varchar(20) DEFAULT NULL, + subject varchar(20) DEFAULT NULL, + score int DEFAULT NULL, +) + +INSERT INTO grades1 VALUES (1, 'lyy', 'Ӣ', 100); +INSERT INTO grades1 VALUES (2, 'lyy', 'ѧ', 98); +INSERT INTO grades1 VALUES (3, 'lyy', '', 97); +INSERT INTO grades1 VALUES (4, 'gss', 'Ӣ', 89); +INSERT INTO grades1 VALUES (5, 'gss', 'ѧ', 88); +INSERT INTO grades1 VALUES (6, 'gss', '', 87); +INSERT INTO grades1 VALUES (7, 'liming', 'Ӣ', 79); +INSERT INTO grades1 VALUES (8, 'liming', 'ѧ', 77); +INSERT INTO grades1 VALUES (9, 'liming', '', 75); + +select * from grades1 + +select score from grades1 where name = 'lyy' +union +select score from grades1 where name = 'gss' +union +select score from grades1 where name = 'liming' + +-------------------------------------------------------- +SELECT name, + + max(CASE subject WHEN 'Ӣ' THEN score ELSE 0 end)Ӣ, + + max(CASE subject WHEN 'ѧ' THEN score ELSE 0 end)ѧ, + + max(CASE subject WHEN '' THEN score ELSE 0 end) + +FROM grades1 + +GROUP BY name + + +---------------------------------------------- +select * from grades1 +pivot( + sum(score) + for name in ([lyy],[gss],[liming]) +)a + +drop table grades1 + + + +---------------------------------------- +--ȡ6000(checkԼ˻>=0)Ҫʹʵ֣޸ȡ¼ʹ +--˻0 + alter table BankCard add constraint CK_Money check(CarMoney >=0) + + begin tran A + declare @Mrr int = 0 + declare @Mr int =0 + + update BankCard set CardMoney = CardMoney-6000 where CardNo = '6225125478544587' + set @Mr += @@error + + insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) + values ('6225125478544587',0,6000,GETDATE()) + + set @Mr += @@error + if @Mrr = 0 + begin + commit tran + print 'ȡɹ' + end + else + begin + rollback transaction + print 'ȡʧ' + end + +----------------------------------------- +--ŷת1000Ԫ(checkԼ˻>=0) + +---ʼ begin tran +begin try + begin tran + declare @myerr int = 0 + declare @TransferMoney int = 1000 + + declare @IdOut int = (select AccountId from AccountInfo where RealName = '') + declare @CardNoOut varchar(20) = (select CardNo from BankCard where AccountId = @IdOut) + + declare @idIn int = (select AccountId from AccountInfo where RealName = 'ŷ') + declare @CardNoIn varchar(20) = (select CardNo from BankCard where AccountId = @idIn) + + + + update BankCard set CardMoney -= @TransferMoney where CardNo = @CardNoOut + set @myerr += @@error + update BankCard set CardMoney +=@TransferMoney where CardNo = @CardNoIn + + insert into CardTransfer values(@CardNoOut,@CardNoIn,@TransferMoney,GETDATE()) + commit +end try + +begin catch + rollback tran +end catch \ No newline at end of file -- Gitee