diff --git "a/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2022-9-29\347\254\224\350\256\260.txt" "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2022-9-29\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..90166f57483e74c0e569a2c102d9a01066d29b1d --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2022-9-29\347\254\224\350\256\260.txt" @@ -0,0 +1,75 @@ + 事务的隔离级别 + +事务的ACID属性 + +​ Atomicity 原子性:同一个事务中的操作,要么全部成功,要么全部失败。 + +​ Consistency 一致性:数据库中的数据保证合法,满足所有的约束(比如唯一约束) + +​ Isolation 隔离性:不同的事务不能相互影响 + +​ Durability 持久性:事务提交后,不管数据库是否崩溃重启,提交的事务所作出的修改都要生效 + +如果不对事务进行隔离,多个事务同时发生在数据库中,将会导致以下的问题: + + 脏读:一个事务读取到另一个未提交事务的修改。没保证原子性,如果当前事务基于这些脏数据进行操作,另一个事务回滚后,这些操作将会是错误的 + 不可重复读:事务中,另外的事务提交了update或delete,导致当前事务前后读取到的数据不一致。没保证持久性,会导致当前事务覆盖另外事务的修改 + 幻读:事务中,另外的事务提交了insert,导致当前事务前后读取到的数据总量不一致。当前事务先对唯一约束进行检查发现没有与将要插入的数据重复的,此时另外的事务提交了,导致当前事务进行插入后数据库中有两条相同的数据,破环了一致性 + + 事务的四种隔离级别 + +read-uncommitted 读未提交 + +​ 可以读到未提交的数据(完全不隔离) + +read-committed 读已提交 + +​ 只能读取已提交的数据(避免脏读) + +repeatable-read 可重复读 + +​ 只能读取到事务开始前,其它事务提交的数据(避免脏读、不可重复读) + +serializable 串行化 + +​ 所有事务一个一个执行,不进行并发(避免脏读、不可重复读、幻读) + + + + + + + 查看事务的隔离级别 +dbcc useroptions + 设置事务隔离级别 +set transaction isolation level <隔离级别> + + +unpivot:列转行函数 + +select name,subject,score from +test +unpivot( +--list = [0,1,2,3] +-- for x in [0,1,2,3] x:0 --> 1 --> 2 -->3 + -- +--for ... in (eng,math,c) +--新列名 for 新列名1 in (原来表的列名) +score for [subject] in (eng,math,chinese) +-- subject in eng --> score +-- subject in math --> score +-- subject in chinese --> score +) as T + + + +pivot:行转列 + +select * from test1 +pivot( +--聚合函数(旧列名) for 旧列名 in (原有数据表的值当做新列名) +count(isvic) for isVic in (胜,负) +) as T1 + + + diff --git "a/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2022-9-30\347\254\224\350\256\260.txt" "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2022-9-30\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..1d13785c70012f8f9adbfbfb1673062e1e4203a0 --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2022-9-30\347\254\224\350\256\260.txt" @@ -0,0 +1,51 @@ + 游标 + + 什么是游标 + +游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成**单个记录处理方式**。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。 + +游标分类: + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 +(2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 +(3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变, + 游标中数据改变,其他列改变,游标中数据不变。 + + + + 游标的使用 + +创建游标: + + +--1.创建游标(Scroll代表滚动游标,不加Scroll则是只进的,只能支持fetch next) +declare <游标名> cursor scroll for select stuname from stuinfo + + +打开游标: + +sql +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 <游标名> --当前位置的上一行 diff --git "a/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery2022-10-6.sql" "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery2022-10-6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ce31e19f5d3056da09ceaead91aed02f58ea0fe9 --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery2022-10-6.sql" @@ -0,0 +1,83 @@ +--ʹαʵּн + select * from People + declare increase cursor scroll + for select PeopleId from People + open increase + declare @gg money=1000,@dd int + fetch first from increase into @dd + while(@@FETCH_STATUS=0) + begin + update People set PeopleSalary +=@gg where PeopleId=@dd + fetch next from increase into @dd + end + + close increase + deallocate increase + select * from People + +--ʹαɾԱ + select * from People + declare delectt cursor scroll for select PeopleId from People + open delectt + declare @mm int + fetch first from delectt into @mm + + while @@FETCH_STATUS =0 + begin + delete from People where PeopleSalary < 6000 + fetch next from delectt into @mm + end + + close delectt + deallocate delectt + + select * from People + +--ʹα ʵ:ABIDֵֶͬôBеʡݣ ޸ijAеijһ£ +create table A( + id int primary key, + Province varchar(20), + City varchar(20) +) + +create table B( + id int primary key, + Province varchar(20), + City varchar(20) +) + +insert into A values(1,'','') +insert into A values(2,'㶫','') +insert into A values(3,'','') +insert into A values(4,'','') + +insert into B values(1,'','ϲ') +insert into B values(3,'','Ͼ') +insert into B values(5,'','人') +insert into B values(7,'','ɳ') + + + +select * from A +select * from B + + + declare cur_City cursor scroll for select a.id,a.province,a.city,b.province,b.city from a join b on a.id = b.id +open cur_City +declare @id int,@aP varchar(10),@aC varchar(10),@bP varchar(10),@bC varchar(10) +fetch first from cur_City into @id,@aP,@aC,@bP,@bC + +while @@FETCH_STATUS = 0 +begin + if @aP != @bP + begin + update b set province = @aP,city = @aC where id = @id + end + fetch next from cur_City into @id,@aP,@aC,@bP,@bC +end + +close cur_City +deallocate cur_City + +select * from A +select * from B \ No newline at end of file