SQLServer实例.docx
《SQLServer实例.docx》由会员分享,可在线阅读,更多相关《SQLServer实例.docx(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、题目11、学校图书馆借书信息管理系统建立三个表:学生信息表:student字段名称数据类型说明stuIDchar(10)学生编号,主键stuNameVarchar(10)学生名称majorVarchar(50)专业图书表:book字段名称数据类型说明BIDchar(10)图书编号,主键titlechar(50)书名authorchar(20)作者借书信息表:borrow字段名称数据类型说明borrowIDchar(10)借书编号,主键stuIDchar(10)学生编号,外键BIDchar(10)图书编号,外键T_timedatetime借书日期B_timedatetime还书日期请编写SQL语
2、句完成以下的功能:1) 查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的学生编号、学生名称、图书编号、图书名称、借出日期;参考查询结果如下图所示:2) 查询所有借过图书的学生编号、学生名称、专业;参考查询结果如下图所示:3) 查询借过作者为“安意如”的图书的学生姓名、图书名称、借出日期、归还日期;参考查询结果如下图所示:4) 查询目前借书但未归还图书的学生名称及未还图书数量;参考查询结果如下图所示:附加:建表语句:USE masterGO/*$建库$*/-检验数据库是否存在,如果为真,删除此数据库-IF exists(SELECT * FROM sysdat
3、abases WHERE name=BOOK) DROP DATABASE BOOKGOCREATE DATABASE BOOKGO-建数据表-USE BOOKGOCREATE TABLE student -学生信息表( stuID CHAR(10) primary key, -学生编号 stuName CHAR(10) NOT NULL , -学生名称 major CHAR(50) NOT NULL -专业)GOCREATE TABLE book -图书表( BID CHAR(10) primary key, -图书编号 title CHAR(50) NOT NULL, -书名 author
4、 CHAR(20) NOT NULL, -作者)GOCREATE TABLE borrow -借书表( borrowID CHAR(10) primary key, -借书编号stuID CHAR(10) foreign key(stuID) references student(stuID), -学生编号BID CHAR(10) foreign key(BID) references book(BID),-图书编号 T_time datetime NOT NULL, -借出日期 B_time datetime -归还日期)GO-学生信息表中插入数据-INSERT INTO student(s
5、tuID,stuName,major)VALUES(1001,林林,计算机)INSERT INTO student(stuID,stuName,major)VALUES(1002,白杨,计算机)INSERT INTO student(stuID,stuName,major)VALUES(1003,虎子,英语)INSERT INTO student(stuID,stuName,major)VALUES(1004,北漂的雪,工商管理)INSERT INTO student(stuID,stuName,major)VALUES(1005,五月,数学)-图书信息表中插入数据-INSERT INTO b
6、ook(BID,title,author)VALUES(B001,人生若只如初见,安意如)INSERT INTO book(BID,title,author)VALUES(B002,入学那天遇见你,晴空)INSERT INTO book(BID,title,author)VALUES(B003,感谢折磨你的人,如娜)INSERT INTO book(BID,title,author)VALUES(B004,我不是教你诈,刘庸)INSERT INTO book(BID,title,author)VALUES(B005,英语四级,白雪)-借书信息表中插入数据-INSERT INTO borrow(
7、borrowID,stuID,BID,T_time,B_time)VALUES(T001,1001,B001,2007-12-26,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T002,1004,B003,2008-1-5,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T003,1005,B001,2007-10-8,2007-12-25)INSERT INTO borrow(borrowID,stuID,BID,T_time,B
8、_time)VALUES(T004,1005,B002,2007-12-16,2008-1-7)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T005,1002,B004,2007-12-22,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T006,1005,B005,2008-1-6,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T007,1002,B001,
9、2007-9-11,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T008,1005,B004,2007-12-10,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T009,1004,B005,2007-10-16,2007-12-18)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T010,1002,B002,2007-9-15,2008-1-5)INSERT
10、 INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T011,1004,B003,2007-12-28,null)INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(T012,1002,B003,2007-12-30,null)标准答案:- 1)查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的学生编号、学生名称、图书编号、图书名称、借出日期select 学生编号=stuID,学生名称=(select stuName from student
11、 where stuID=borrow.stuID),图书编号=BID,图书名称=(select title from book where BID=borrow.BID),借出日期=T_time from borrow where stuID in (select stuID from student where major=计算机) and T_time2007-12-15 and T_time2*(select count(*) from ProWage where Wage=PWage)update ProWage set total=total+AWage,Wage=Wage+AWa
12、geelsebreakendprint一共加薪:+convert(varchar,total)+元print加薪后的程序员工资列表:select * from ProWage-调用存储过程1-exec Sum_wage PWage=2000,AWage=100,total=0exec Sum_wage PWage=2200,AWage=100,total=0exec Sum_wage PWage=3000,AWage=100,total=0exec Sum_wage PWage=4000,AWage=100,total=0exec Sum_wage PWage=5000,AWage=100,t
13、otal=0exec Sum_wage PWage=6000,AWage=100,total=0-2、创建存储过程2-if exists (select * from sysobjects where name=Avg_wage)drop procedure Avg_wageGOcreate procedure Avg_wage PWage int,AWage int,total intas while (1=1)beginif (select Avg(Wage) from ProWage)=PWage)update ProWage set total=total+AWage,Wage=Wag
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SQLServer 实例
限制150内