西南科技大学数据库实验二(Oracle 11g)

/* *实验二 数据查询 *时间:2019-11-25 *联系:2838215550@qq.com *附言:把代码写成一首诗。 *SELECT [ ALL | DISTINCT] < 目标列表达式 > [,< 目标列表达式 > ]... *FROM <表名或视图名> [,<表名或视图名>]... *[ WHERE < 条件表达式 > ] *[ GROUP BY < 列名1> [ HAVING < 条件表达式 > ]] *[ ORDER BY < 列名2> [ASC | DESC ]] * *alter table 书目 rename column 单位 to 出版单位; */

--基础实验
1、 查询“红楼梦”目前可借的各图书编号,及所属版本信息。(是否借出为‘否‘的图书)
select 图书.图书编号,图书.ISBN from 图书,书目 where 图书.ISBN=书目.ISBN and 书目.书名='红楼梦' and 图书.是否借出=' 否';

2、查找高等教育出版社的全部书目及单价,结果按单价降序排序。
select 书目.书名,书目.单价 from 书目 where 书目.出版单位='高等教育出版社' order by 书目.单价 desc;

3、统计“红楼梦”各版的藏书数量(ISBN不一样则版本不一样)。
select 图书.ISBN,count(图书.图书编号) 数量 from 图书 join 书目 on 图书.ISBN=书目.ISBN where 书目.书名='红楼梦' group by 图书.ISBN;

4、查询学号“20061234”号借书证借阅未还的图书的信息。
select * from 书目 where 书目.ISBN in (select 图书.ISBN from 图书 join 借阅 on 图书.图书编号=借阅.图书编号 where 借阅.借书证号='20061234');
select * from 书目 where ISBN in (select ISBN from 图书 where 图书编号 in (select 图书编号 from 借阅 where 借阅.借书证号='20061234'));

5、查询各个出版社的图书最高单价、平均单价。
select 出版单位,max(单价) 最高价格,avg(单价) 平均价格 from 书目 group by 出版单位;

6、要查询借阅了两本和两本以上图书的读者的我的信息。
select * from 读者 where 借书证号 in (select 借书证号 from 借阅 group by 借书证号 having count(图书编号)>=2);

7、查询“王菲”的单位、所借图书的书名和借阅日期。
select 读者.单位,书目.书名,to_char(借阅.借书日期,'yyyy/mm/dd') 借书日期 from 书目,图书,读者,借阅 where 书目.ISBN=图书.ISBN and 图书.图书编号=借阅.图书编号 and 借阅.借书证号=读者.借书证号 and 读者.姓名='王菲';

8、查询每类图书的册数和平均单价。
select 图书分类.类名,count(*) 册数,avg(书目.单价) 平均单价 from 书目,图书分类 where 书目.图书分类号=图书分类.图书分类号 group by 图书分类.类名;

9、统计从未借书的读者人数。
select count(*) 从未借书读者人数 from 读者 where 读者.借书证号 not in (select distinct 借阅.借书证号 from 借阅);

10、统计参与借书的人数。
select count(*) 从未借书读者人数 from 读者 where 读者.借书证号 in (select distinct 借阅.借书证号 from 借阅);

11、找出全部借书未还的读者的信息及所借图书编号及名称。
select 读者.*,借阅.图书编号,书目.书名 from 读者,借阅,书目,图书 where 读者.借书证号=借阅.借书证号 and 借阅.图书编号=图书.图书编号 and 图书.ISBN=书目.ISBN and 借阅.归还日期 is null;

12、检索书名是以“Internet”开头的全部图书的书名和做者。
select 书名,做者 from 书目 where 书名='Internet%'

13、查询各图书的罚款总数。
select 借阅.图书编号,sum(罚款分类.罚金) 罚款总数 from 借阅,罚款分类 where 借阅.罚款分类号=罚款分类.罚款分类号 group by 借阅.图书编号;

14、查询借阅及罚款分类信息,若是有罚款则显示借阅信息及罚款名称、罚金,若是没有罚款则罚款名称、罚金显示空(左外链接)。
select * from 借阅 left join 罚款分类 on 借阅.罚款分类号=罚款分类.罚款分类号;

15、查询借阅了全部“文学”类书目的读者的姓名、单位。
select 读者.姓名,读者.单位 from 读者,借阅 where 读者.借书证号=借阅.借书证号 and 借阅.图书编号 in (select 图书.图书编号 from 图书,书目,图书分类 where 图书分类.类名='文学' and 图书分类.图书分类号=书目.图书分类号 and 图书.ISBN=书目.ISBN);




--扩展实验
1、在书目关系中新增“出版年份”,并在该属性下添加数据。(使用SQL完成)。
--alter table 书目 add(出版年份 date);
--alter table 书目 add 出版年份 date;
alter table 书目 add (出版年份 number(4,0);
update 书目 set 出版年份=2005 where ISBN=7040195836;
update 书目 set 出版年份=1983 where ISBN=9787508040110;
update 书目 set 出版年份=2008 where ISBN=9787506336239;
update 书目 set 出版年份=2009 where ISBN=9787010073750;

2、求总藏书量、藏书总金额、最高价、最低价。
select count(图书编号) 总藏书量,sum(单价) 藏书总金额,max(单价) 最高价,min(单价) 最低价 from 图书,书目 where 图书.ISBN=书目.ISBN;

3、列出藏书在5本以上的书目(书名、做者、出版社、出版年份)。
select 书名,做者,出版单位,出版年份 from 书目 where ISBN in (select 图书.ISBN from 图书 group by 图书.ISBN having count( 图书.isbn)>5);

4、列出年份最久远的书?
select 书名,做者,出版单位,出版年份 from 书目 where 出版年份 in (select min(书目.出版年份) from 书目);

5、 目前实际已借出多少册书?
select count(借阅流水号) 借出数量 from 借阅 where 归还日期 is null;

6.、哪一年的图书最多?
--嵌套太深,报错:分组函数的嵌套太深
--select 出版年份,count(出版年份) 数量 from 图书,书目 where 图书.ISBN=书目.ISBN group by 出版年份 having count(出版年份)>max(count(出版年份));
select * from (select 出版年份,count(出版年份) 数量 from 图书,书目 where 图书.ISBN=书目.ISBN group by 出版年份 order by count(出版年份) desc ) where rownum=1;


7、 哪本借书证未归还的图书最多?
select * (select 借书证号 from 借阅 where 归还日期 is null group by 借书证号 order by count(借书证号) desc) where rownum=1;

8、平均每本借书证的借书册数。
select 借书证号, count(借书证号) from 借阅 group by 借书证号;

9、哪一个单位的读者平均借书册数最多?
select 读者.单位 from 读者 where 读者.借书证号 in (select 借书证号 from (select 借书证号, count(借书证号) from 借阅 group by 借书证号 order by count(借书证号) desc) where rownum=1)

10、 最近两年都未被借过的书。
select 图书.图书编号,书目.书名,书目.做者 from 图书,书目,借阅 where 图书.图书编号=借阅.图书编号 and 书目.ISBN=图书.ISBN and 借阅.借书日期 not between to_date('20180101','yyyy/mm/dd') and to_date('20191231','yyyy/mm/dd');

11、今年未借过书的借书证。
select 读者.借书证号 from 读者 where 读者.借书证号 not in (select 借阅.借书证号 from 借阅 where 借阅.借书日期 between to_date('20050101','yyyy/mm/dd') and to_date('20191231','yyyy/mm/dd'));
--select 读者.借书证号 from 读者 where not exists (select 借阅.借书证号 from 借阅 where 借阅.借书日期 between to_date('20050101','yyyy/mm/dd') and to_date('20191231','yyyy/mm/dd'));