1、数据库原理与应用庞国莉题目+答案1、 设有一图书馆数据库,包括三个表:图书表、读者表、借阅表。三个表的结构如表3-3、表3-4和表3-5所示。完成以下习题。表3-3图书表结构列名说明数据类型约束图书号图书唯一的图书号定长字符串,长度为20 主键 书名图书的书名变长字符串,长度为50空值作者图书的编著者名变长字符串,长度为30空值 出版社 图书的出版社变长字符串,长度为30空值 单价出版社确定的图书的单价浮点型,FLOAT空值 表3- 4读者表结构列名说明数据类型约束说明读者号读者唯一编号定长字符串,长度为10 主键 姓名读者姓名定长字符串,长度为8非空值性别读者性别定长字符串,长度为2非空值办
2、公电话读者办公电话定长字符串,长度为8空值 部门 读者所在部门变长字符串,长度为30空值 表3- 5借阅表结构列名说明数据类型约束说明读者号读者的唯一编号定长字符串,长度为10 外码,引用读者表的主键 图书号图书的唯一编号定长字符串,长度为20 外码,引用图书表的主键 借出日期 图书借出的日期定长字符串,长度为8,为yymmdd 非空值 归还日期图书归还的日期定长字符串,长度为8,为yymmdd空值主键为:(读者号, 图书号)(1)用SQL语句创建图书馆数据库。Create database 图书馆数据库;(2)用SQL语句创建上述三个表。use 图书馆数据库 Gocreate table b
3、ook ( bnum char (20) primary key, bname varchar (50), bwri varchar (30), bpub varchar (30), bpri float );use 图书馆数据库 gocreate table rea ( rnum char (10) primary key, rname char (8) not null, rsex char (2) not null, rphone char (8), rpar char(30) );use 图书馆数据库 gocreate table br ( rnum char (10) not nul
4、l, bnum char (20) not null, brdate char (8) not null, brback char (8), foreign key (rnum) references rea(rnum), foreign key (bnum) references book(bnum) );(3)基于图书馆数据库的三个表,用SQL语言完成以下各项操作:1)给图书表增加一列“ISBN”,数据类型为CHAR(10)alter table book add ISBN CHAR(10);2)为刚添加的ISBN列增加缺省值约束,约束名为ISBNDEF,缺省值为7111085949;AL
5、TER TABLE BOOKADD CONSTRAINT ISBNDEF DEFAULT 7111085949 FOR ISBN;3)为读者表的办公电话列,添加一个CHECK约束,要求前五位88320,约束名为CHECKDEF。ALTER TABLE REAADD CONSTRAINT CHECKDEF CHECK (RPHONE =88320000 AND RPHONE 2order by (count (bnum) desc(6)针对以上三个表,用SQL语言完成以下各项多表连接查询、子查询、组合查询1)查询读者的基本信息以及他/她借阅的情况。select rea.* ,br.bnum ,b
6、r.brback ,br.brdate from rea,brwhere rea.rnum = br.rnum 2)查询读者的读者号、姓名、借阅的图书名、借出日期、归还日期。select br.bnum ,rea.rname,book.bname,br.brback ,br.brdate from rea,br,book where rea.rnum = br.rnum and book.bnum=br.bnum 3)查询借阅了机械工业出版社出版,并且书名中包含数据库三个字的图书的读者,显示读者号、姓名、书名、出版社,借出日期、归还日期。select rea.rnum,rea.rname,bo
7、ok.bname,br.brback ,br.brdate from rea,br,book where book.bname like %数据库% and bpub = 机械工业出版社 and rea.rnum = br.rnum and book.bnum=br.bnum 4)查询至少借阅过1本机械工业出版社出版的书的读者的读者号、姓名、书名,借阅本数,并按借阅本数多少降序排列。select br.rnum,rea.rname,bname,COUNT(br.bnum )本数from book,rea,brwhere rea.rnum = br.rnum and book.bnum = br
8、.bnum andbook.bnum = 机械工业出版社Group by br.rnum ,rname,bnamehaving COUNT(bname) =1order by COUNT(br.bnum) desc 5)查询与王平的办公电话相同的读者的姓名。select r1.rnamefrom rea r1,rea r2where r1.rname = 王平 and r1.rphone = r2.rphone6)查询办公电话为88320701的所有读者的借阅情况,要求包括借阅了书籍的读者和没有借阅的读者,显示他们的读者号、姓名、书名、借阅日期。select rea.rnum,rname,bn
9、ame,brdatefrom book,rea,brwhere rphone = 88320701 and rea.rnum = br.rnum and book.bnum = br.bnum 7)查询所有单价小于平均单价的图书号、书名、出版社select bnum,bname,bpubfrom book where bpri (select MAX (bpri) rom book where bpub = 机械工业出版社)9)查询科学出版社的图书中单价比机械工业出版社最低单价高的的图书书名、单价。select bname ,bprifrom bookwhere bpub = 科学出版社 an
10、d bpri (select Min (bpri) from book where bpub = 机械工业出版社)10)查询已被借阅过并已归还的图书信息。select *from bookwhere bnum in ( select bnum from br where brback is not null)11)查询从未被借阅过的图书信息。select *from bookwhere bnum not in ( select bnum from br )12)查询正在借阅的图书信息。select *from bookwhere bnum in ( select bnum from br wh
11、ere brdate not like null and brback like null )13)查询借阅了机械工业出版社出版的书名中含有数据库书三个字的图书、或者借阅了科学出版社出版的书名中含有数据库书三个字的图书的读者姓名、书名。select rname,bnamefrom book,rea,brwhere bname like %数据库% and rea.rnum in ( select br.rnum from br,book where bpub = 机械工业出版社 or bpub = 科学出版社 )14)查询借阅了机械工业出版社出版的书名中含有数据库书三个字的图书并且也借阅了科学
12、出版社出版的书名中含有数据库书三个字的图书的读者姓名、书名。select rname,bnamefrom book,rea,brwhere bname = %数据库% and rea.rnum in ( select br.rnum from br r1,br r2,book where r1.rnum = r2.rnum and book.bnum =br.bnum and bpub = 机械工业出版社 and bpub = 科学出版社 ) 15)查询借阅了机械工业出版社出版的书名中含有数据库书三个字的图书但没有借阅了科学出版社出版的书名中含有数据库书三个字的图书的读者姓名、书名。select rname,bnamefrom book,rea,brwhere bname like %数据库% and rea.rnum in ( select br.rnum from br ,book where br.bnum = book.bnum and bpub = 机械工业出版社 and book.bnum in (select br.rnum from br,book where book.bnum = br.bnum and bname like %数据库% and bpub = 科学出版社) )
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1