From 96995a1475c1cc24b3ec67fb489af23cc9faf534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?09=E9=99=88=E7=83=BD=E6=9D=B0sql=E8=BF=9B=E9=98=B6?= <3241892760@qq.com> Date: Wed, 21 Sep 2022 12:18:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../9.20\346\270\270\346\240\207.md" | 193 ++++++++++++++++++ .../9.20\346\270\270\346\240\207.md" | 72 +++++++ 2 files changed, 265 insertions(+) create mode 100644 "09\351\231\210\347\203\275\346\235\260/\344\275\234\344\270\232/9.20\346\270\270\346\240\207.md" create mode 100644 "09\351\231\210\347\203\275\346\235\260/\347\254\224\350\256\260/9.20\346\270\270\346\240\207.md" diff --git "a/09\351\231\210\347\203\275\346\235\260/\344\275\234\344\270\232/9.20\346\270\270\346\240\207.md" "b/09\351\231\210\347\203\275\346\235\260/\344\275\234\344\270\232/9.20\346\270\270\346\240\207.md" new file mode 100644 index 0000000..cecda47 --- /dev/null +++ "b/09\351\231\210\347\203\275\346\235\260/\344\275\234\344\270\232/9.20\346\270\270\346\240\207.md" @@ -0,0 +1,193 @@ +``` +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/09\351\231\210\347\203\275\346\235\260/\347\254\224\350\256\260/9.20\346\270\270\346\240\207.md" "b/09\351\231\210\347\203\275\346\235\260/\347\254\224\350\256\260/9.20\346\270\270\346\240\207.md" new file mode 100644 index 0000000..f9040cf --- /dev/null +++ "b/09\351\231\210\347\203\275\346\235\260/\347\254\224\350\256\260/9.20\346\270\270\346\240\207.md" @@ -0,0 +1,72 @@ +## 游标 + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 (2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 (3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + +**创建游标:** + +``` +--1.创建游标(Scroll代表滚动游标,不加Scroll则是只进的,只能支持fetch next) +declare <游标名> cursor scroll for select stuname from stuinfo +``` + +**打开游标:** + +``` +open <游标名> +``` + +**关闭游标:** + +``` +close <游标名> +``` + +**删除游标:** + +``` +deallocate <游标名> +``` + +# 提取数据 + +## 第一行 + +fetch **first** from 游标名 + +## 最后一行 + +fetch **last** from 游标名 + +## 第n行 + +fetch **absolute** n from 游标名 + +## 当前位置第n行 + +fetch **relative** n from 游标名 + +## 当前位置下一行 + +fetch **next** from 游标名 + +## 当前位置的上一行 + +fetch **prior** from 游标名 + +## 可进行填充后打印输出 + +# 游标指向某行多列数据 + +declare 游标名 cursor [scroll] for (select 列1,列2,列3,列4 from 表名) + +把数据赋给变量 + +fetch **first** from 游标名 into 变量1,变量2,变量3,变量4 + +# @@FETCH_STATUS + +@@FETCH_STATUS=0,提取成功 + +@@FETCH_STATUS=-1,提取失败 + +@@FETCH_STATUS=-2,行不存在 -- Gitee