diff --git "a/34\351\237\246\344\273\262\346\231\223/\344\275\234\344\270\232/9.23\347\264\242\345\274\225.sql" "b/34\351\237\246\344\273\262\346\231\223/\344\275\234\344\270\232/9.23\347\264\242\345\274\225.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a15c212504d7158254de482d8582a189b2ef9342 --- /dev/null +++ "b/34\351\237\246\344\273\262\346\231\223/\344\275\234\344\270\232/9.23\347\264\242\345\274\225.sql" @@ -0,0 +1,19 @@ + +select * from tb_bibliography; +select * from tb_book; +select * from tb_inf_student; +select * from tb_record; +select * from tb_student; +--1.创建tb_student(name)索引 填充因子设为50 +create nonclustered index IX_name on tb_student(name) +with( + --填充因子 + fillfactor = 50 +) +--2.创建tb_record(borrow_time,return_time) 使用索引查询没还书的同学并且让没还书的同学将图书归还 +create index IX_time on tb_record(borrow_time,return_time) +select * from tb_record with (index=IX_time) where borrow_time is not null and return_time is null +update tb_record set return_time =GETDATE() where return_time is null +--3.增加新列id() 创建聚集索引tb_book(id) + alter table tb_book add id int IDENTITY(1,1) NOT NULL + create unique clustered index IX_id on tb_book(id) \ No newline at end of file diff --git "a/34\351\237\246\344\273\262\346\231\223/\344\275\234\344\270\232/9.26\345\233\276\350\247\206.sql" "b/34\351\237\246\344\273\262\346\231\223/\344\275\234\344\270\232/9.26\345\233\276\350\247\206.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c0980e0009f7d8b6c51ff5a97ce308ce1057047f --- /dev/null +++ "b/34\351\237\246\344\273\262\346\231\223/\344\275\234\344\270\232/9.26\345\233\276\350\247\206.sql" @@ -0,0 +1,38 @@ + +--1)编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +go +create view V_newab(卡号,身份证,姓名,余额) +as +select BankCard.CardNo,AccountInfo.AccountCode,AccountInfo.RealName,BankCard.CardMoney from BankCard left join AccountInfo on BankCard.AccountId=AccountInfo.AccountId +go +--2)行转列常用做法: group by + sum(case when) /+count(case when) 数据分析+ over (paritition by, order by) +insert into record values +(1991,1,1.1), +(1991,2,1.2), +(1991,3,1.3), +(1991,4,1.4), +(1992,1,2.1), +(1992,2,2.2), +(1992,3,2.3), +(1992,4,2.4) +select * from record +go +create view V_RErecord(year,m1,m2,m3,m4) +as + select * from record + pivot (sum(amount) for month in ([1],[2],[3],[4])) as RErecord + go + select * from V_RErecord + --3)列转行 + create table sc ( + name varchar(10), + english int , + maths int , + music int + ) +insert into sc values ('Jim',90,88,99) + select name,'english' subject,english as score from sc + union + select name,'maths' subject,maths as score from sc + union + select name,'music' subject,music as score from sc \ No newline at end of file diff --git "a/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/2022.9.23\347\264\242\345\274\225.md" "b/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/2022.9.23\347\264\242\345\274\225.md" new file mode 100644 index 0000000000000000000000000000000000000000..3f5f863fb5c80c7cf5183402b9aed8b798f01160 --- /dev/null +++ "b/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/2022.9.23\347\264\242\345\274\225.md" @@ -0,0 +1,53 @@ +**SQL 索引(Index)** + +用于提高数据表的查询速度。一个表可以创建多个索引,一个索引可以包含一个或者多个字段。 + +- 使用索引可以加快数据访问速度. +- 两种类型的索引分别是聚集索引和非聚集索引. +- 使用create index使用索引可以加快数据访问速度. +- 两种类型的索引分别是聚集索引和非聚集索引. +- 使用CREATE INDEX可以为指定的表创建索引.可以为指定的表创建索引. + +## 语法 + +在某个字段上创建索引的基本语法如下: + +``` +create index index_name +ON table_name ( column1, column2.....); +``` + +index_name 是索引的名字,以后在删除索引时会用到。 + +## 示例 + +使用 SQL 语句创建一个包含七列的 website 表: + +``` +create table website ( +id int not null auto_increment, +name varchar(20) not null, +url varchar(20), +age tinyint unsigned not null, +alexa int unsigned not null, +uv float default'0', +country CHAR(3) not null, +PRIMARY KEY (`id`) +); + +现在您可以针对 name 字段创建索引,用以提高检索姓名时的效率,如下所示: +``` + +``` +CREATE INDEX myIndexON website(name); +``` + +​ myIndex 是索引的名字。 + +## 删除索引 + +删除索引请使用下面的 SQL 语句: + +``` +ALTER TABLE websiteDROP INDEX myIndex; +``` \ No newline at end of file diff --git "a/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/9.28\344\272\213\345\212\241.md" "b/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/9.28\344\272\213\345\212\241.md" new file mode 100644 index 0000000000000000000000000000000000000000..7d36c151efd214b59a00d351c8d24975743557ce --- /dev/null +++ "b/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/9.28\344\272\213\345\212\241.md" @@ -0,0 +1,68 @@ +# **事务** + + + +**事务( Transaction)由一次或者多次基本操作构成,或者说,事务由一条或者多条 SQL 语句构成。** + +**事务中的所有 SQL 语句是一个整体,共同进退,不可分割,要么全部执行成功,要么全部执行失败** + + + +**--与事务控制有关的 SQL 命令包括:** +**--begin transaction 或者 begin tran: 开始事务;** +**--commit tran/transaction: 提交事务;** +**-- rollback tran/transaction:回滚事务;** +**--savepoint: 在事务内部设置回滚标记点;** +**--release savepoint re:删除回滚标记点;** +**--rollback to:将事务回滚到标记点(rollback 命令的一种变形写法)。** + +一个事务要么提交(Commit),要么回滚(Rollback),提交意味着成功,回滚意味着失败。 + +编写事务代码时,以begin命令开头,后跟一条或者多条 SQL 语句,最后书写 commit或者 rollback 命令;commit和 rollback 对应事务的两种状态,只能出现一个。 + +事务控制命令仅能与 DML 类别的 SQL 命令一起使用,包括 insert、update、delete和 select,在创建或者删除表时不能使用事务,因为这些操作在数据库中是自动提交的。 + + + +#### 事务的属性(ACID) + +--事务特性:原子性,一致性,隔离性,持久性 + +一般来说,事务具有四个标准属性,分别是原子性、一致性、隔离性、持久性,简称 **ACID**。 + +具体说明如下: + +##### 1) 原子性(**A**tomicity,或称不可分割性) + +一个事务中的所有 SQL 语句,要么全部执行成功,要么全部执行失败,不会结束在中间的某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。 + +##### 2) 一致性(**C**onsistency) + +在事务开始之前和事务结束以后,数据库的完整性没有被破坏。这表示写入的数据必须完全符合所有的预设规则,其中包含数据的精确度、串联性以及后续数据库可以自发性地完成预定的工作。 + +##### 3) 隔离性(**I**solation,又称独立性) + +数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。事务隔离分为不同级别,包括读未提交(Read uncommitted)、读提交(read committed)、可重复读(repeatable read)和串行化(Serializable)。 死锁 操作系统 + +##### 4) 持久性(**D**urability) + +事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。 + + + +**--事务的4个隔离级别:** +-- 读未提交: 脏读,不可重复读,幻读 +-- 读已提交: 不可重复读,幻读 +-- 可重复读: 部分幻读 +-- 串行化 + + + +#### 事务执行流程 + +![](https://gitee.com/snailclass/tuchuang/raw/master/img/image-20220916113630148-2022-9-1611:40:42.png) + + + + + diff --git "a/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/9.30\346\270\270\346\240\207.md" "b/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/9.30\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..893b656ef1c23d484946d77a1ea9e287b38f9a5e --- /dev/null +++ "b/34\351\237\246\344\273\262\346\231\223/\347\254\224\350\256\260/9.30\346\270\270\346\240\207.md" @@ -0,0 +1,141 @@ +## 游标(Cursor) + +#### 什么是游标 + +游标它使用户可逐行访问由SQL Server返回的结果集。 + +使用游标的一个主要的原因就是把集合操作转换成**单个记录处理方式**。用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 <游标名> --当前位置的上一行 +``` + +例1:使用游标输出部门编号为009的员工和工资 + +例2:使用游标输出各部门的部门号,最高工资,和最低工资 + +例3:使用游标实现加薪操作 + +例4:使用游标删除不达标员工 + + + + + + + +**提取数据给变量以供它用(取出第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 <游标名> +``` + + + + +