From 7985771c7414242da81a9f02255c94987878276c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Thu, 6 Oct 2022 10:21:35 +0800 Subject: [PATCH 1/4] =?UTF-8?q?31=E5=90=B4=E6=AC=A3=E7=87=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/2022-10-06.md" | 35 +++++++++++ .../\344\272\213\345\212\241.md" | 59 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" create mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" new file mode 100644 index 0000000..3a90ce9 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" @@ -0,0 +1,35 @@ +``` +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 +--假设刘备取款6000,(添加check约束,设置账户余额必须>=0),要求:使用事务实现,修改余额和添加取款记录两步操作使用事务 +--update BankCard set CardMoney=6000 where CardNo=6225125478544587; + +alter table bankcard +add check(cardmoney>=0); + +begin transaction t_bank +update BankCard set CardMoney=CardMoney-6000 where CardNo=6225125478544587 +if @@ERROR!=0 +rollback transaction +insert into CardExchange values(6225125478544587,0,6000,GETDATE()) +if @@ERROR!=0 +rollback transaction +else +commit transaction t_bank +--刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) +begin transaction t_banktransfer +update BankCard set CardMoney=CardMoney-1000 where CardNo=6225125478544587 +if @@ERROR!=0 +rollback transaction +insert into CardTransfer values(6225125478544587,6225547854125656,1000,GETDATE()) +if @@ERROR!=0 +rollback transaction +update BankCard set CardMoney=CardMoney+1000 where CardNo=6225547854125656 +if @@ERROR!=0 +rollback transaction +else +commit transaction t_banktransfer +``` \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" new file mode 100644 index 0000000..67a26bb --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" @@ -0,0 +1,59 @@ +### 事务 + +**事务( Transaction)由一次或者多次基本操作构成,或者说,事务由一条或者多条 SQL 语句构成。** + +**事务中的所有 SQL 语句是一个整体,共同进退,不可分割,要么全部执行成功,要么全部执行失败。** + +#### 事务的属性(ACID) + +##### 1) 原子性 **A**tomicity + +一个事务中的所有 SQL 语句,要么全部执行成功,要么全部执行失败,不会结束在中间的某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态 + +##### 2) 一致性 **C**onsistency + +在事务开始之前和事务结束以后,数据库的完整性没有被破坏。这表示写入的数据必须完全符合所有的预设规则,其中包含数据的精确度、串联性以及后续数据库可以自发性地完成预定的工作。 + +##### 3) 隔离性 **I**solation + +数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。 + +##### 4) 持久性 **D**urability + +事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。 + +### + +``` +--开始事务 +begin tran +--错误代码 +declare @err int = 0 + +--事务提交:如果语句都执行成功, commit tran/transaction +if (@myerr = 0) + begin + commit transaction A + end +--事务回滚:如果事务中有一条语句执行失败, rollback tran/transaction +else + begin + rollback transaction B + end + + --select * from Products +--print @@error --错误码:0:执行成功 非0的时候代表该语句有错误 + +--标记事务回滚起点 : save tran B + +--方法2: +----begin try +---- --事务的开始:begin tran +---- --sql语句 +---- commit --提交 +----end try + +----begin catch --捕获错误,如果某条语句执行错误,将被捕获 +---- rollback tran +----end +``` \ No newline at end of file -- Gitee From 1db30f896387b9b785b0b27faebfcb010a039f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Thu, 6 Oct 2022 05:23:50 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2031?= =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95/=E4=BD=9C=E4=B8=9A/2022-10-06.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/2022-10-06.md" | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" deleted file mode 100644 index 3a90ce9..0000000 --- "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-10-06.md" +++ /dev/null @@ -1,35 +0,0 @@ -``` -select * from AccountInfo --个人信息 -select * from BankCard --银行卡信息 -select * from CardStateChange --银行卡状态更改表 -select * from CardTransfer --转账表 -select * from CardExchange --交易记录 ---假设刘备取款6000,(添加check约束,设置账户余额必须>=0),要求:使用事务实现,修改余额和添加取款记录两步操作使用事务 ---update BankCard set CardMoney=6000 where CardNo=6225125478544587; - -alter table bankcard -add check(cardmoney>=0); - -begin transaction t_bank -update BankCard set CardMoney=CardMoney-6000 where CardNo=6225125478544587 -if @@ERROR!=0 -rollback transaction -insert into CardExchange values(6225125478544587,0,6000,GETDATE()) -if @@ERROR!=0 -rollback transaction -else -commit transaction t_bank ---刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) -begin transaction t_banktransfer -update BankCard set CardMoney=CardMoney-1000 where CardNo=6225125478544587 -if @@ERROR!=0 -rollback transaction -insert into CardTransfer values(6225125478544587,6225547854125656,1000,GETDATE()) -if @@ERROR!=0 -rollback transaction -update BankCard set CardMoney=CardMoney+1000 where CardNo=6225547854125656 -if @@ERROR!=0 -rollback transaction -else -commit transaction t_banktransfer -``` \ No newline at end of file -- Gitee From e6dac6685ec4f66394e71d1c16b451bdeb8f5065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Thu, 6 Oct 2022 05:24:05 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2031?= =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95/=E7=AC=94=E8=AE=B0/=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\272\213\345\212\241.md" | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" deleted file mode 100644 index 67a26bb..0000000 --- "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\344\272\213\345\212\241.md" +++ /dev/null @@ -1,59 +0,0 @@ -### 事务 - -**事务( Transaction)由一次或者多次基本操作构成,或者说,事务由一条或者多条 SQL 语句构成。** - -**事务中的所有 SQL 语句是一个整体,共同进退,不可分割,要么全部执行成功,要么全部执行失败。** - -#### 事务的属性(ACID) - -##### 1) 原子性 **A**tomicity - -一个事务中的所有 SQL 语句,要么全部执行成功,要么全部执行失败,不会结束在中间的某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态 - -##### 2) 一致性 **C**onsistency - -在事务开始之前和事务结束以后,数据库的完整性没有被破坏。这表示写入的数据必须完全符合所有的预设规则,其中包含数据的精确度、串联性以及后续数据库可以自发性地完成预定的工作。 - -##### 3) 隔离性 **I**solation - -数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。 - -##### 4) 持久性 **D**urability - -事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。 - -### - -``` ---开始事务 -begin tran ---错误代码 -declare @err int = 0 - ---事务提交:如果语句都执行成功, commit tran/transaction -if (@myerr = 0) - begin - commit transaction A - end ---事务回滚:如果事务中有一条语句执行失败, rollback tran/transaction -else - begin - rollback transaction B - end - - --select * from Products ---print @@error --错误码:0:执行成功 非0的时候代表该语句有错误 - ---标记事务回滚起点 : save tran B - ---方法2: -----begin try ----- --事务的开始:begin tran ----- --sql语句 ----- commit --提交 -----end try - -----begin catch --捕获错误,如果某条语句执行错误,将被捕获 ----- rollback tran -----end -``` \ No newline at end of file -- Gitee From ee79aff69b74b2734b9d5c4acc4598e084e36589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Thu, 6 Oct 2022 13:37:21 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\270\270\346\240\207.md" | 100 ++++++++++++++++++ .../2022-10-06\346\270\270\346\240\207.md" | 67 ++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" create mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-06\346\270\270\346\240\207.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" new file mode 100644 index 0000000..015a3e0 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" @@ -0,0 +1,100 @@ +``` +use test_trigger +go + +select * from People +select * from Department + +--例3:使用游标实现加薪操作 +declare cur_id cursor scroll +for select PeopleId from People + +open cur_id + +declare @p_Id int,@p_salary money = 2000 +fetch first from cur_id into @p_Id + +while(@@FETCH_STATUS = 0) +begin + update People set PeopleSalary += @p_salary where PeopleId = @p_Id + fetch next from cur_id into @p_Id +end + +select * from people + +close cur_id + +--例4:使用游标删除不达标员工 +declare cur_del cursor scroll +for select PeopleId,PeopleSalary from People + +open cur_del + +declare @pId int,@pSalary money +fetch first from cur_del into @pId,@pSalary + +while(@@FETCH_STATUS = 0) +begin + if(@pSalary < 10000) + begin + delete People where PeopleId = @pId + fetch next from cur_del into @pId,@pSalary + end + + fetch next from cur_del into @pId,@pSalary +end + +select * from people + +close cur_del + +--使用游标 实现:如果A表与B表中ID字段值相同,那么将B表中的省份,城市 修改成与A表中的城市一致(连表) +create database AB +go +use AB +go +create table A( +aId int not null, +aProvince varchar(50), +aCity varchar(50) +) +go +create table B( +bId int not null, +bProvince varchar(50), +bCity varchar(50) +) +go +insert into A(aId,aProvince,aCity) values +(1,'安徽','淮南'), +(6,'广东','深圳'), +(7,'福建','龙岩'), +(8,'甘肃','兰州') +go +insert into B(bId,bProvince,bCity) values +(1,'河北','石家庄'), +(2,'广东','汕头'), +(3,'福建','厦门'), +(4,'山西','太原') +go + +--deallocate cur_city +declare cur_city cursor scroll +for select A.* from A join B on A.aId = B.bId + +open cur_city + +declare @aid int,@apro varchar(50),@acity varchar(50) +fetch first from cur_city into @aid,@apro,@acity + +while(@@FETCH_STATUS = 0) +begin + update B set B.bProvince = @apro,B.bCity = @acity where B.bId = @aid + fetch next from cur_city into @aid,@apro,@acity +end + +select * from A +select * from B + +close cur_city +``` \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-06\346\270\270\346\240\207.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-06\346\270\270\346\240\207.md" new file mode 100644 index 0000000..a455c1f --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-06\346\270\270\346\240\207.md" @@ -0,0 +1,67 @@ +### 游标 Cursor + +游标可以使用户逐行访问由SQL Server返回的结果集,能把集合操作转换成**单个记录处理方式**,按照用户自己的意愿来显示和处理这些记录。 + +#### 游标分类: + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 (2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 (3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + +#### 游标的使用 + +``` +--创建游标(Scroll代表滚动游标,不加Scroll则是只进的,只能支持fetch next) +declare <游标名> cursor scroll for select stuname from stuinfo + +--打开游标: +open <游标名> + +--关闭游标 +close <游标名> + +--删除游标 +deallocate <游标名> + +--提取数据操作 +fetch first from <游标名> --结果集的第一行 +fetch last from <游标名> --最后一行 +fetch absolute 1 from <游标名> --从游标的第一行开始数,第n行。 +fetch relative 3 from <游标名> --从当前位置数,第n行。 +fetch next from <游标名> --当前位置的下一行 +fetch prior from <游标名> --当前位置的上一行 +``` + +**提取数据给变量以供它用(取出第3行学生姓名,查询该学生详细信息):** + +``` +declare @vari varchar(20) +fetch absolute 3 from <游标名> into @vari +select * from StuInfo where stuName = @vari +``` + +**利用游标提取所有的学生信息:** + +``` +--方案一: +fetch absolute 1 from <游标名> +while @@FETCH_STATUS = 0 --@@FETCH_STATUS=0,提取成功,-1提取失败,-2行不存在 + begin + fetch next from <游标名> + end + +--方案二: +declare @myvar varchar(20) +fetch first from <游标名> into @myvar +while @@FETCH_STATUS = 0 + begin + print '提取成功'+@myvar + fetch next from <游标名> into @myvar + end +``` + +**利用游标修改和删除数据:** + +``` +--更新语法: +-- fetch absolute 3 from <游标名> +-- update 语句 where Current of <游标名> +``` \ No newline at end of file -- Gitee