diff --git "a/23\346\236\227\344\270\226\346\266\233/\344\275\234\344\270\232/2022.09.28\344\275\234\344\270\232.sql" "b/23\346\236\227\344\270\226\346\266\233/\344\275\234\344\270\232/2022.09.28\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..33d7bf8b24b0d27043a34f23219c6272da89d010 --- /dev/null +++ "b/23\346\236\227\344\270\226\346\266\233/\344\275\234\344\270\232/2022.09.28\344\275\234\344\270\232.sql" @@ -0,0 +1,89 @@ +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 score from grades1 where name = 'lyy' +union +select score from grades1 where name = 'gss' +union +select score from grades1 where name = 'liming' + +create view vgg +as +select name,subject,score from grades1 +go +select * from vgg pivot(sum(score) for subject in ([英语],[数学],[语文]))b + +use BankTest +--假设刘备取款6000,(添加check约束,设置账户余额必须>=0),要求:使用事务实现,修改余额和添加取款记录两步操作使用事务 + +declare @money int=6000 +declare @name varchar(20)='刘备' +declare @CardNo int=(select CardNo from Bankcard where AccountId=(select AccountId from AccountInfo where RealName=@name)) +declare @myerr int=0 + +begin transaction a + +alter table BankCard add constraint lala check(CardMoney>=0) +update BankCard set CardMoney-=@money where CardNo=@CardNo +set @myerr+=@@ERROR +insert into CardExchange (CardNo,MoneyOutBank,ExchangeTime) values (@CardNo,@money,GETDATE()) +set @myerr+=@@ERROR + +if(@myerr=0) +begin + commit transaction a +end +else +begin + rollback transaction a +end + +--刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) + +select * from BankCard +select * from CardTransfer +select * from AccountInfo + +declare @money int=1000 +declare @outname varchar(20)='刘备' +declare @inname varchar(20)='张飞' +declare @CardoutNo int=(select CardNo from Bankcard where AccountId=(select AccountId from AccountInfo where RealName=@outname)) +declare @CardinNo int=(select CardNo from Bankcard where AccountId=(select AccountId from AccountInfo where RealName=@inname)) +declare @myerr int=0 + +begin transaction b + +alter table BankCard add constraint lala check(CardMoney>=0) +set @myerr+=@@ERROR +update BankCard set CardMoney-=@money where CardNo=@CardoutNo +set @myerr+=@@ERROR +update BankCard set CardMoney+=@money where CardNo=@CardinNo +set @myerr+=@@ERROR +insert into CardTransfer(CardNoOut,CardNoIn,TransferMoney,TransferTime) values (@CardoutNo,@CardinNo,@money,GETDATE()) +set @myerr+=@@ERROR + +if(@myerr=0) +begin + commit transaction a +end +else +begin + rollback transaction a +end \ No newline at end of file diff --git "a/23\346\236\227\344\270\226\346\266\233/\347\254\224\350\256\260/2022.09.28\347\254\224\350\256\260.md" "b/23\346\236\227\344\270\226\346\266\233/\347\254\224\350\256\260/2022.09.28\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..4da6b0535cc237bb6a9fadfefab50f3387e2e63e --- /dev/null +++ "b/23\346\236\227\344\270\226\346\266\233/\347\254\224\350\256\260/2022.09.28\347\254\224\350\256\260.md" @@ -0,0 +1,219 @@ +## 回顾 + +- 使用索引可以加快数据访问速度. +- 两种类型的索引分别是聚集索引和非聚集索引. +- 使用CREATE INDEX可以为指定的表创建索引. +- 视图是一个虚表,通常用来查看数据库中一个或多个表的数据. +- 使用CREATE VIEW可以创建视图 + + + +## 事务 + + + +#### 什么是事务 + +**事务( 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) 持久性 + +事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。 + + + +#### 事务执行流程 + +![](D:\系统缓存\桌面\图片\image.png) + + + + + +#### 事务分类 + +**自动提交事物**: 是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语句,其它语句照常运行读写数据库。 + + + + + + + +## 游标 + +#### 什么是游标 + +游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成**单个记录处理方式**。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。 + +游标分类: + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 +(2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 +(3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + + + +#### 游标的使用 + +**创建游标:** + +```sql +--1.创建游标(Scroll代表滚动游标,不加Scroll则是只进的,只能支持fetch next) +declare <游标名> cursor scroll for select stuname from stuinfo +``` + +**打开游标:** + +```sql +open <游标名> +``` + +**关闭游标:** + +``` +close <游标名> +``` + +**删除游标:** + +``` +deallocate <游标名> +``` + + + +**提取数据操作:** + +```sql +fetch first from <游标名> --结果集的第一行 +fetch last from <游标名> --最后一行 +fetch absolute 1 from <游标名> --从游标的第一行开始数,第n行。 +fetch relative 3 from <游标名> --从当前位置数,第n行。 +fetch next from <游标名> --当前位置的下一行 +fetch prior from <游标名> --当前位置的上一行 +``` + + + +**提取数据给变量以供它用(取出第3行学生姓名,查询该学生详细信息):** + +```sql +declare @vari varchar(20) +fetch absolute 3 from <游标名> into @vari +select * from StuInfo where stuName = @vari +``` + +**利用游标提取所有的学生信息:** + +```sql +--方案一: +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 +``` + +**利用游标修改和删除数据:** + +```sql +--更新语法: +-- fetch absolute 3 from <游标名> +-- update 语句 where Current of <游标名> + + +--练习1:修改周飘的班级id为1 + +--练习2:使用删除姓名为曾鹏的个人信息 + +``` + +**创建游标指向某行多列数据,并循环显示数据:** + +```sql +--此处如果指向所有数据,可以将for后面的语句修改成select * from stuinfo +declare <游标名> cursor scroll +declare mycursor cursor scroll for select stuname,StuSex,ClassID from stuinfo + +open <游标名> + +declare @name varchar(20) +declare @sex varchar(2) +declare @cid int +fetch first from mycursor into @name,@sex,@cid +while @@FETCH_STATUS=0 +fetch next from <游标名> into into @name,@sex,@cid + +while @@FETCH_STATUS = 0 --@@FETCH_STATUS=0,提取成功,-1提取失败,-2行不存在 + begin + print '提取成功' + fetch next from CURSORMember into@into @name,@sex,@cid + end +close <游标名> +``` + + + +## 练习 + +**事务练习(BankTest):** + + + +1.--假设刘备取款6000,(添加check约束,设置账户余额必须>=0),要求:使用事务实现,修改余额和添加取款记录两步操作使用事务 + +2.--刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) + +![](https://gitee.com/snailclass/tuchuang/raw/master/img/image-20220916113716667-2022-9-1611:40:44.png) + + + +**游标练习** + +- 创建学生游标,该游标包含(学生姓名,兴趣爱好,生源地,荣誉总数) +- 循环遍历161开头的学生信息 +- 使用游标统计生源地为北京的荣誉总数 + +- 合理使用游标和事务,让5-1号前借书的学生将图书归还 + + \ No newline at end of file