《数据库软件SQL SERVER》A卷
考试说明:在F:盘新建一个以考生的学号和姓名命名的文件夹(例如2007001张三),将所做的数据库以及所有的SQL文本文件均存放在此文件夹中。(第一题为数据库,第二、三、四和五题均存为一个SQL文本文件,请标明题目编号)
书名和借阅日期
“library”表记录如下: bookid bname author price 100001 C程序王宇天 24 设计 100002 VB 张大海 19 程序设 计语言 100003 大学宋美美 34 英语 100004 机床张小梅 27 加工 class publish edition content 计算机 中国水利2003第使用通俗出版社 1版 语言介绍C程序语言 计算机 清华大学2004第 出版社 2版 一、(30分)操作题
创建数据库:在该文件夹下创建一个为“library”的数据库,主文件的初始大小为3MB,增长方式为10%增长,最大容量为10MB。日志文件初始大小为3MB,增长方式为1MB,最大容量为10MB。 “library”中包含的数据“book”表有如下结构:(bookid为主键) 字段名称 数据类字段允许DEFAULCHECK表约束名 备注 bookid bname author price class publish edition content 型 char char char int Char char char char 大小 20 20 20 10 30 10 200 空 √ √ √ √ √ √ T 字段大小 20 20 2 20 字段大小 20 20 4 8 达式 大于零 ck_price 允许空 √ √ √ 允许空 √ √ 书编号 书名 作者 价钱 类别 英语 建筑 北大出版社 电子工业出版社 2010第 1版 2009第 1版 “reader”表记录 readerid rname 1001 1002 1003 1004 陈芳 罗军军 黎明 刘凡易 sex 女 女 男 男 department 计算机 机电 外语 信息 “reader”表结构:(readerid为主键) 字段名称 readerid rname sex department 字段名称 bookid readerid status date 数据类型 char char char char 数据类型 char char char datetime 出版社 版本号 内容简介 “borrow”表记录 bookid readerid 100001 100002 100003 100004
1001 1004 1004 1002 status 借阅 续借 借阅 借阅 date 2012-5-30 2013-2-13 2013-1-19 2013-10-22 备注 读者编号 读者姓名 性别 所在部门 备注 书编号 读者编号 状态 借阅日期 “borrow”表结构(bookid和readerid为主键) 二、(40分)编写查询语句(要求所有命令存储于SQL的文本文件中,文件名为“SQL代码”)
(1)向“book”表中添加一条记录,书号为100005,书名计算机网络基础,作者孙超
(2)修改“reader”表中记录,将姓刘凡易的所在部门改为水建 (3)查询所有未借阅图书的所有读者姓名 (4)查询至少借阅了两门图书的读者的姓名
(1) 将“book”表中的记录导入到“book.txt”文件中 (2) 创建一视图view_borrow,要求包含借阅者的姓名、所借阅图
教研室主任签字: 第 1页 (本试卷共 2 页 )
borrow,reader where borrow.readerid=reader.readerid groupby department --2.7
select bname from book b1,book b2 where b1.bname='大学英语'and b2.publish=b1.publish and b2.bname<>'大学英语' --2.8
select bookid from book where price<(selectavg(price)from book) --2.9 三、(10分)编写存储过程
select bname,author from book where bname like'%计算机%' 要求利用读者姓名查询出该读者所借阅图书的书名、出版社和价格,
--2.10查询各个出版社中图书最高价格、最低价格和图书数目 并给出“罗军军”读者的相关信息。
select publish,max(price)as 最高价格,min(price)as 最低价格,count(*)as 图四、(10分)编写触发器
为“borrow”表创建一触发器,保证插入记录图书编号和读者编号书数目 from book where publish isnotnullgroupby publish
--3.存储过程,要求利用读者姓名查询出该读者所借阅图书的书名、出版社和价格,必须存在于book和reader表中。
并给出“罗军军”读者的相关信息。 五、(10分)编写一内嵌表值函数
use library 要求根据图书名,查询所有借阅该图书的读者信息,包括:readerid、
ifobject_id('pro_1','p')isnotnull rname、sex和department。
dropprocedure pro_1 六、T-SQL程序题
go
createprocedure pro_1(@rname char(20))
AS --2.1
insertinto book(bookid,bname,author)values('100005','计算机网络基础','孙BEGIN
select bname,publish,price from reader,borrow,book 超')
where reader.readerid=borrow.readerid --2.2
and borrow.bookid=book.bookid and rname=@rname update reader set department='水建'where rname='刘凡易'
END --2.3
GO select rname from reader leftjoin borrow on reader.readerid=borrow.readerid
exec pro_1'罗军军' where bookid isnull
--4.触发器,为“borrow”表创建一触发器,保证插入记录图书编号和读者编号必--2.4
须存在于book和reader表中。 select rname from borrow,reader where borrow.readerid=reader.readerid
ifobject_id('tri_1','tr')isnotnull groupby borrow.readerid,rname havingcount(*)>=2
droptrigger tri_1 --2.5
GO select rname from borrow b1,borrow b2,reader where b1.readerid=b2.readerid
tri_1 on borrow forinsert and b1.bookid='100001'and b2.bookid='100002'andCREATETRIGGER
AS b1.readerid=reader.readerid
BEGIN --2.6
select department,count(distinct borrow.readerid)as 借阅人数 from ifexists(select*from inserted
(5)查询借阅了“100001”和未借阅“100002”图书的读者姓名
(6)查询出借阅了图书的各个部门的人数
(7)查询和“大学英语”出版社相同的图书名称 (8)查询比所有图书平均价低的图书的编号 (9)查询书名中包含有“计算机”的书名和作者
(10)查询各个出版社中图书最高价格、最低价格和图书数目
where inserted.bookid notin(select bookid from book)
or inserted.readerid notin(select readerid from reader)) begin
rollbacktransaction
PRINT'图书表或读者表中没有相关的记录' end end go
insertinto borrow(bookid,readerid)values('991101','00000') --5.内嵌表值函数
--要求根据图书名,查询所有借阅该图书的读者信息,包括:readerid、rname、sex和department。
ifobject_id('fun_1','if')isnotnull dropfunction fun_1 GO
createfunction fun_1(@bname char(20)) RETURNSTABLE AS RETURN (
select reader.readerid,rname,sex,department from reader,borrow,book where reader.readerid=borrow.readerid
and borrow.bookid=book.bookid and bname=@bname ) GO
select*from fun_1('C程序设计')
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库SQL上机模拟习题在线全文阅读。
相关推荐: