diff --git "a/40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.09.20 \347\254\254\345\215\201\350\212\202\350\257\276-\346\270\270\346\240\207.md" "b/40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.09.20 \347\254\254\345\215\201\350\212\202\350\257\276-\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..770163cffe4fa29c2df526ccce5589962fd73b2f --- /dev/null +++ "b/40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.09.20 \347\254\254\345\215\201\350\212\202\350\257\276-\346\270\270\346\240\207.md" @@ -0,0 +1,195 @@ +```sql +create database STUDENTS +go +use STUDENTS +go + +CREATE TABLE tb_student ( + stu_num char(8) primary key, + name char(20) NOT NULL, + gender bit DEFAULT NULL, + birth date DEFAULT NULL, + school char(20) DEFAULT NULL, + major char(20) DEFAULT NULL, +); +go +insert into tb_student values('16100101','马诗',1,'1998-03-16','理学院','统计'); +insert into tb_student values('16130201','博文',0,'1998-04-24','信息学院','计算机科学与技术'); +insert into tb_student values('16130203','魏波',1,'1998-10-29','信息学院','计算机科学与技术'); +insert into tb_student values('16130205','黄弘',1,'1998-08-06','信息学院','数据科学与大数据技术'); +insert into tb_student values('17100104','易高明',0,'1999-05-29','理学院','信息与计算科学'); +insert into tb_student values('17100105','万承承',0,'1999-09-11','理学院','信息与计算科学'); +insert into tb_student values('17110101','黄弘',0,'2000-07-25','文法学院','法学'); +insert into tb_student values('17130202','邹睿睿',1,'1998-06-29','信息学院','计算机科学与技术'); +insert into tb_student values('17130204','马又云',1,'1999-03-27','信息学院','数据科学与大数据技术'); +insert into tb_student values('18100103','邓承明',1,'2000-07-25','理学院','信息与计算科学'); + + +CREATE TABLE tb_inf_student( + stu_num char(8) primary key foreign key references tb_student(stu_num), + hobby varchar(120) DEFAULT NULL, + speciality varchar(120) DEFAULT NULL, + ori_loca char(16) DEFAULT NULL, + prize int DEFAULT NULL, +); +go + +insert into tb_inf_student values('16100101','听音乐','艺术特长','北京',2); +insert into tb_inf_student values('16130201','看小说',null,'湖南',3); +insert into tb_inf_student values('16130203','硬笔书法','艺术特长','新疆',1); +insert into tb_inf_student values('16130205','听音乐','艺术特长','北京',2); +insert into tb_inf_student values('17100104','打篮球',null,'北京',3); +insert into tb_inf_student values('17100105','编程','科技特长','北京',2); +insert into tb_inf_student values('17110101','打篮球','科技特长','河北',2); +insert into tb_inf_student values('17130202','编程','科技特长','天津',3); +insert into tb_inf_student values('17130204','看电影',null,'北京',1); +insert into tb_inf_student values('18100103',null,null,'河南',null); +go + + +CREATE TABLE tb_bibliography ( + ISBN char(13) primary key, + name char(32) DEFAULT NULL, + author char(32) DEFAULT NULL, + publishing char(32) DEFAULT NULL, + pub_time date DEFAULT NULL, + intro varchar(200) DEFAULT NULL, + category char(8) DEFAULT NULL, + price money DEFAULT NULL, +); +go + +insert into tb_bibliography values('7040409659','大学计算机','李凤霞','高等教育出版社','2014','教育部大学计算机课程改革项目规划教材','TP',28); +insert into tb_bibliography values('7301046065','刑法学','高明轩','北京大学出版社','2000','刑法_法学高等学校中国教材','O',69); +insert into tb_bibliography values('7806553312','射雕英雄传','金庸','广州出版社','2018','金庸作品集','I',67.9); +insert into tb_bibliography values('9788020002207','红楼梦','曹雪芹','人民文学出版社','2008','学术研究或个人阅读都非常合适','I',31.25); +insert into tb_bibliography values('9787113254100','Python语言及其应用','赵广辉','中国铁道出版社','2019','本书介绍Python语言的基础知识及其在各个领域的具体应用','TP',62.2); +insert into tb_bibliography values('9787115266156','管理信息系统实用教程(第2版)','王若宾','人民邮电出版社','2012','普通高等教育\十一五\国家级规划教材','TP',36); +insert into tb_bibliography values('9787115356840','管理信息系统实用教程(第3版)','王若宾','人民邮电出版社','2015','普通高等教育\十一五\国家级规划教材','TP',45); +insert into tb_bibliography values('9787302252955','人工智能:一种现代的方法(第3版)','黄今夏','清华大学出版社','2011','《人工智能:一种现代的方法(第3版)》为大学计算机教育著名教材系列之一','TP',132.6); +insert into tb_bibliography values('9787513030953','信息论','田甜','知识产权出版社','2015','文理科公选课指定教材','TP',126); +insert into tb_bibliography values('9787569302585','计算统计(第2版)','冯新奇','西安交通大学出版社','2018','本书涵盖了计算统计的所有核心内容','TP',67.5); +insert into tb_bibliography values('9789113268712','新编数据库技术','王若宾','中国铁道出版社','2018','本书重构了课程内容结构','TP',38.5); + + + +CREATE TABLE tb_book( + barcode char(9) primary key, + ISBN char(13) foreign key references tb_bibliography(ISBN), + [status] bit default null, +); + +insert into tb_book values('O924.01','7301046065',0); +insert into tb_book values('O924.02','7301046065',1); +insert into tb_book values('O924.03','7301046065',1); +insert into tb_book values('I13.212','9788020002207',1); +insert into tb_book values('I13.213','9788020002207',1); +insert into tb_book values('I247.56','7806553312',1); +insert into tb_book values('I247.59','7806553312',0); +insert into tb_book values('TP122.32','9787569302585',1); +insert into tb_book values('TP122.33','9787569302585',1); +insert into tb_book values('TP311.11','9787113254100',1); +insert into tb_book values('TP311.12','9787113254100',0); +insert into tb_book values('TP311.13','9787115356840',1); +go + + +CREATE TABLE tb_record( + rid int primary key identity(1,1), + stu_num char(8) references tb_student(stu_num), + barcode char(9) references tb_book(barcode), + borrow_time datetime DEFAULT NULL, + return_time datetime DEFAULT NULL, +); +go + +insert into tb_record values('16130203','I247.56','2019-04-09','2019-04-13'); +insert into tb_record values('17130204','I247.56','2019-04-15','2019-04-17'); +insert into tb_record values('16130205','I247.59','2019-04-17','2019-04-20'); +insert into tb_record values('16100101','I247.56','2019-04-17','2019-04-18'); +insert into tb_record values('17100105','TP311.11','2019-04-29',null); +insert into tb_record values('16130201','I247.59','2019-05-01','2019-05-20'); +insert into tb_record values('17130202','TP311.12','2019-05-03',null); +insert into tb_record values('18100103','I13.212','2019-05-04','2019-05-15'); +insert into tb_record values('18100103','I13.213','2019-05-20','2019-05-30'); +insert into tb_record values('17110101','O924.01','2019-05-25',null); +go + + +select * from tb_student +select * from tb_inf_student +select * from tb_bibliography +select * from tb_book +select * from tb_record + +--select name,author,category from tb_bibliography +--where author,category = (select author,category from tb_bibliography where name='管理信息系统实用教程(第3版)') + + +--游标练习 +--- 创建学生游标,该游标包含(学生姓名,兴趣爱好,生源地,荣誉总数) +declare student cursor scroll for(select name,hobby,ori_loca,prize from tb_student ts inner join tb_inf_student ti on ts.stu_num=ti.stu_num) +declare @names varchar(10),@hobbys varchar(10),@loca varchar(10),@prizes int + +open student +fetch first from student into @names,@hobbys,@loca,@prizes +while(@@FETCH_STATUS=0) +begin + fetch next from student into @names,@hobbys,@loca,@prizes + print @names+','+@hobbys+','+@loca+','+convert(varchar(20),@prizes) +end + +close student + +--- 循环遍历161开头的学生信息 +declare stu_information cursor scroll for(select * from tb_student where stu_num like '161%') +declare @num int,@name varchar(10),@gender int,@birth date,@school varchar(10),@major varchar(20) + +open stu_information +fetch first from stu_information into @num,@name,@gender,@birth,@school,@major +while(@@FETCH_STATUS=0) +begin + fetch next from stu_information into @num,@name,@gender,@birth,@school,@major + print convert(varchar(2),@num)+','+@name+','+convert(varchar(2),@gender)+','+convert(varchar(20),@birth)+','+@school+','+@major +end +close stu_information + +--- 使用游标统计生源地为北京的荣誉总数 +declare loca cursor scroll for(select sum(prize) from tb_inf_student where ori_loca='北京') +declare @locas int +open loca +fetch first from loca into @locas +while(@@FETCH_STATUS=0) +begin + fetch next from loca into @locas + print @locas +end +close loca + +--- 合理使用游标和事务,让5-1号前借书的学生将图书归还 +declare books cursor scroll for(select * from tb_record where borrow_time<'2019-05-01' and return_time is null) +open books +begin transaction + + declare @nums int=0 + update tb_record set return_time=GETDATE() where borrow_time<'2019-05-01'and return_time is null + set @nums += @@ERROR + +print @nums +if @nums>0 + begin + print '未归还' + rollback transaction + end +else + begin + print '已归还' + commit transaction + end + + +close books +select * from tb_record + +``` + diff --git "a/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.05 \347\254\254\344\270\211\350\212\202\350\257\276-\345\217\230\351\207\217\357\274\210\345\261\200\351\203\250\345\217\230\351\207\217\344\270\216\345\205\250\345\261\200\345\217\230\351\207\217\357\274\211.md" "b/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.05 \347\254\254\344\270\211\350\212\202\350\257\276-\345\217\230\351\207\217\357\274\210\345\261\200\351\203\250\345\217\230\351\207\217\344\270\216\345\205\250\345\261\200\345\217\230\351\207\217\357\274\211.md" index 1b1b723e1fc456ee8049bd9532f1cb8e26f0a402..8c4f81fcaa953590b5e4114e74f13c3a5f3557cb 100644 --- "a/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.05 \347\254\254\344\270\211\350\212\202\350\257\276-\345\217\230\351\207\217\357\274\210\345\261\200\351\203\250\345\217\230\351\207\217\344\270\216\345\205\250\345\261\200\345\217\230\351\207\217\357\274\211.md" +++ "b/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.05 \347\254\254\344\270\211\350\212\202\350\257\276-\345\217\230\351\207\217\357\274\210\345\261\200\351\203\250\345\217\230\351\207\217\344\270\216\345\205\250\345\261\200\345\217\230\351\207\217\357\274\211.md" @@ -114,5 +114,3 @@ print @局部变量 --显示自动编号 print '当前自动编号的值:' + convert(varchar(10),@@IDENTITY) ``` - -## diff --git "a/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.20 \347\254\254\345\215\201\350\212\202\350\257\276-\346\270\270\346\240\207.md" "b/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.20 \347\254\254\345\215\201\350\212\202\350\257\276-\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..350de15e2b0001e07a8983702098e437fb8fc70f --- /dev/null +++ "b/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.09.20 \347\254\254\345\215\201\350\212\202\350\257\276-\346\270\270\346\240\207.md" @@ -0,0 +1,117 @@ +### 一、游标 + +#### 1、概念 + +* 游标是一种能从包含多个元组的集合中每次读取一个元组的机制。游标总是和一段SELECT语句关联,SELECT语句查询出的结果集就作为集合,游标能每次从该集合中读取出一个元组进行不同操作。 + + + +#### 2,分类 + +* **静态游标(Static)**:在操作游标的时候,数据发生变化,游标中数据不变 +* **动态游标(Dynamic)**:在操作游标的时候,数据发生变化,游标中数据改变,默认值。 +* **键集驱动游标(KeySet)**:在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + + + +#### 3、优势 + +- 实现对单条记录的处理 +- 提供删除和处理基于游标位置的数据表中的数据记录的能力 +- 是面向集合的数据库管理系统和应用程序设计二者之间的联系桥梁 +- 游标允许程序对查询语句返回记录的结果集中的每一行数据信息执行相同或不同的操作,而不是一次性的对结果集整体进行统一操作 + +- 将游标定位在结果集特定元组。 + +- 将游标指定结果集中的元组数据读出。 +- 利用循环读取结果集中的多个元组数据。 +- 对游标指定结果集的元组进行数据修改。 +- 为其它用户设置结果集数据的更新限制。 +- 提供脚本、存储过程和触发器中访问结果集中数据的TSQL语句。 + + + +#### 4、声明游标 + +```sql +declare 游标名 cursor scroll for (查询语句) +``` + +##### (1)**scroll** + +* 指定游标使用的读取选项,默认值为NEXT,如果不使用该关键字,那么读取游标只能进行NEXT操作,如果使用该关键字,那么游标向任何方向,或者任何位置移动,进行next、last、first、prior、relative、absulute 操作。 + +#### 5、打开游标(open) + +```sql +declare 游标名 cursor scroll for (查询语句) +open 游标名 +``` + +#### 6、关闭游标(close) + +在处理完操作后,必须关闭游标释放结果集 + +```sql +//操作完成后关闭游标 +close 游标名 +``` + + + +#### 7、读取游标 + +```sql +fetch first/next/last from 游标名 into () +``` + +| 名称 | 含义 | +| ------------------- | ------------------------------------------------------------ | +| **==first==** | 读取游标中的第一行 | +| **==last==** | 读取游标的最后一行 | +| **==next==** | 读取游标当前行下一行数据 | +| **==prior==** | 读取当前游标上一行数据 | +| **==relative n==** | 读取游标当前行之前或之后第n行数据(n为正则向前,反之则向后) | +| **==absulute n==** | 读取游标第n行数据(n为负从最后一行开始,反之则从第一行开始) | + + + +#### 8、删除游标 + +```sql +deallocate 游标名 +``` + + + +#### 9、游标系统变量与函数 + +* **@@CURSOR_NUM**返回最后打开的游标中满足条件的元组数。 + +- **@@FETCH_STATUS** 返回上次执行FETCH命令的状态。 + +| 返回值 | 说明 | +| ------ | --------------------------------- | +| 0 | fetch语句成功 | +| -1 | fetch语句失败或此元组不在结果集中 | +| -2 | 被读取的元组不存在 | + + + +#### 10,列 + +```sql +declare student cursor scroll for(select name,hobby,ori_loca,prize from tb_student ts inner join tb_inf_student ti on ts.stu_num=ti.stu_num) +declare @names varchar(10),@hobbys varchar(10),@loca varchar(10),@prizes int + +open student +fetch first from student into @names,@hobbys,@loca,@prizes +while(@@FETCH_STATUS=0) +begin + fetch next from student into @names,@hobbys,@loca,@prizes + print @names+','+@hobbys+','+@loca+','+convert(varchar(20),@prizes) +end + +close student +``` +