select* from 书目,图书 where 书目.ISBN=图书.ISBN and 书名='红楼梦' and 是否借出='否'
查找高等教育出版社的所有书目及单价,结果按单价降序排序。
1 2 3 4
select* from 书目 where 出版单位='高等教育出版社' orderby 单价 desc;
统计“红楼梦”各版的藏书数量(ISBN不同则版本不同)。
1 2 3 4
selectcount(ISBN),ISBN from 书目 where 书名='红楼梦' groupby ISBN;
查询学号“20061234”号借书证借阅未还的图书的信息。
1 2 3 4 5 6 7
select 借书证号,书名,书目.ISBN ,出版单位,作者,单价, 图书分类号 from 书目,借阅,图书 where 书目.ISBN=图书.ISBN and 借阅.图书编号=图书.图书编号 and 是否借出='是' and 归还日期 isNULL and 借书证号=20061234;
查询各个出版社的图书最高单价、平均单价。(同上)
1 2 3
select 出版单位,max(单价),avg(单价) from 书目 groupby 出版单位;
要查询借阅了两本和两本以上图书的读者的个人信息。
1 2 3 4 5 6 7
select*from 读者 where 借书证号 in ( select 借书证号 from 借阅 groupby 借书证号 havingcount(借书证号)>=2);
查询“王菲”的单位、所借图书的书名和借阅日期。
1 2 3 4 5 6
select 单位,书名,借书日期 from 读者,借阅,书目,图书 where 姓名='王菲' and 读者.借书证号=借阅.借书证号 and 借阅.图书编号=图书.图书编号 and 图书.ISBN=书目.ISBN ;
查询每类图书的册数和平均单价。
1 2 3
selectcount(图书分类号),avg(单价) from 书目 groupby 图书分类号;
统计从未借书的读者人数。
1 2 3 4
selectcount(姓名) from 读者 wherenotexists (select*from 借阅 where 读者.借书证号=借阅.借书证号);
统计参与借书的人数。
1 2 3 4 5
selectcount(姓名) from 读者 whereexists ( select*from 借阅 where 读者.借书证号=借阅.借书证号);
找出所有借书未还的读者的信息及所借图书编号及名称。
1 2 3 4 5 6
selectdistinct 借阅.借书证号,姓名,单位,性别,地址,联系电话,身份证编号,借阅.图书编号,书名 from 借阅,书目,读者,图书 where 借阅.借书证号=读者.借书证号 and 借阅.图书编号=图书.图书编号 and 书目.ISBN=图书.ISBN and 归还日期 isnull;
检索书名是以“红”开头的所有图书的书名和作者。
1 2 3
select 书名,作者 from 书目 where 书名 like'红%';
查询各图书的罚款总数。(分组,图书编号+罚款总数)
1 2 3 4 5
select 书目.ISBN,sum(罚金) from 借阅,罚款分类,书目,图书 where 借阅.罚款分类号=罚款分类.罚款分类号 and 借阅.图书编号=图书.图书编号 groupby 书目.ISBN
select 读者.姓名,读者.单位 from 读者 wherenotexists( select* from 书目,图书分类 where 书目.图书分类号=图书分类.图书分类号 and 图书分类.类名='文学'andnotexists ( select* from 借阅,图书 where 借阅.图书编号=图书.图书编号 and 借阅.借书证号=读者.借书证号 and 书目.ISBN=图书.ISBN ) )
实验扩展
在书目关系中新增“出版年份”,并在该属性下添加数据。(使用SQL完成)
ISBN
书名
作者
出版单位
出版年份
单价
图书分类号
7040195836
数据库系统概论
王珊
高等教育出版社
2005
39.00
200
9787508040110
红楼梦
曹雪芹
人民出版社
1983
20.00
100
9787506336239
红楼梦
曹雪芹
作家出版社
2008
34.30
100
9787010073750
心学之路
张立文
人民出版社
2009
33.80
300
1 2 3 4 5 6
altertable 书目 add 出版年份 number; update 书目 set 出版年份=2005where ISBN=7040195836; update 书目 set 出版年份=1983where ISBN=9787508040110; update 书目 set 出版年份=2008where ISBN=9787506336239; update 书目 set 出版年份=2009where ISBN=9787010073750;
2、求总藏书量、藏书总金额,总库存册数、最高价、最低价。
1 2 3
selectcount(图书编号),sum(单价),max(单价),min(单价) from 书目,图书 where 书目.ISBN=图书.ISBN;
3、列出藏书在5本以上的书目(书名、作者、出版社、出版年份)。
1 2 3 4 5 6 7 8
select 书名,作者,出版单位,出版年份 from 书目 where 书目.ISBN in ( select 图书.ISBN from 图书 groupby 图书.ISBN havingcount( 图书.ISBN)>5 );
4、列出年份最久远的书?
1 2 3 4 5 6 7
select 书名,作者,出版单位,出版年份 from 书目 where 书目.出版年份 in ( selectmin(书目.出版年份) from 书目 )
5、目前实际已借出多少册书?
1 2 3
selectcount(借阅.借阅流水号) 借出数量 from 借阅 where 归还日期 isnull;
6、哪一年的图书最多?
1 2 3 4 5 6 7
select*from ( select 出版年份,count(出版年份) 数量 from 图书,书目 where 图书.ISBN = 书目.ISBN groupby 出版年份 orderbycount(出版年份) desc ) where rownum=1;
7、 哪本借书证未归还的图书最多?
1 2 3 4 5 6 7
select*from ( select 借书证号 from 借阅 where 归还日期 isnull groupby 借书证号 orderbycount(借书证号) desc ) where rownum=1
8、平均每本借书证的借书册数。
1 2 3
select 借书证号,count(借书证号) from 借阅 groupby 借书证号;
9、哪个单位的读者平均借书册数最多?
1 2 3 4 5 6 7 8 9
select 读者.单位 from 读者 where 读者.借书证号 in ( select 借书证号 from ( select 借书证号, count(借书证号) from 借阅 groupby 借书证号 orderbycount(借书证号) desc ) where rownum=1);
10、最近两年都未被借过的书。
1 2 3 4 5 6 7
selectdistinct(书目.ISBN),书目.书名,书目.出版单位 from 图书,书目,借阅 where 图书.图书编号=借阅.图书编号 and 书目.ISBN=图书.ISBN and 借阅.借书日期 notbetween to_date('20180101','yyyy/mm/dd') and to_date('20191231','yyyy/mm/dd');