SQL实验习题解答

更新时间:2023-11-16 03:18:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

SQL实验7-9习题解答

习题7

1、查询1986年出生的读者姓名、身份证号。

select 姓名,身份证号 from 读者

where mid(身份证号,7,4)='1986' ; &&从身份证号字段中取出出生年份 2、查询在国际学院工作的读者编号、姓名和性别。

select 读者编号,姓名,性别 from 读者

where 工作单位 like '国际学院';

3、查询没有归还图书的读者编号、读者姓名和读者工作单位。

select 读者编号,姓名,工作单位 from 读者

where 读者编号 in

(select 读者编号 from 借阅 where not 是否归还);

4、查询每种类别的图书的分类号、最高价格和平均价格,并按最高价格的降序输出。

select 分类号,max(价格) max(价格),avg(价格) from 图书 group by 分类号

order by max(价格) desc;

5、查询Java图书的详细信息。

select * from 图书 where 图书名 like '*Java*';

6、查询在1992年到2000年之间入库的图书编号、出版时间、入库时间和图书名称,并按入库时间排序输出。

select 图书编号,出版时间,入库时间,图书名 from 图书 order by 入库时间;

7、查询没有借阅图书编号以001开头的读者编号和姓名。

select 读者编号,姓名 from 读者 where 读者编号 not in

(select 读者编号 from 借阅 where 图书编号 like '001*');

8、查询借阅了001-000029图书编号的读者编号、图书编号、借书日期。 select 读者编号,图书编号,借阅日期 from 借阅 where 图书编号='001-000029'; 9、查询没有借书的读者姓名。

select 姓名 from 读者

where 读者编号 not in

(select 读者编号 from 借阅);

10、查询图书类别为001号图书的种类和图书数量。

select count(*),sum(图书数量) from 图书

where 分类号='001';

11、查询每种图书的入库数量。

select 分类号,sum(图书数量) from 图书

group by 分类号;

12、查询既借阅了'001-000010图书又借阅了'005-000018图书的读者借书信息。

select * from 借阅

where 图书编号='001-000010' and 读者编号 in (select 读者编号 from 借阅

where 图书编号='005-000018');

习题8

1、查询文学类的图书基本信息。

select a.* from 图书 a,图书分类 b

where a.分类号 = b.分类号 and 分类号.图书名='文学类';

2、查询读者马永强借阅的图书编号、图书名称、借书日期和归还日期。

select b.图书编号,图书图书名,借阅日期,归还日期 from 读者 a,图书 b,借阅 c

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号

and 姓名='马永强';

3、查询国际学院没有归还图书的读者编号、读者名称、图书名称、借书日期和归还日期。

select a.读者编号,姓名,图书图书名,借阅日期,归还日期

from 读者 a,图书 b,借阅 c

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号

and 工作单位 like '国际学院' and是否归还=no;

4、查询借阅了清华大学出版社出版的图书的读者编号、读者名称、图书名称、借书日期和归还日期。

select a.读者编号,姓名,图书图书名,借阅日期,归还日期 from 读者 a,图书 b,借阅 c

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号 and 出版社名 = '清华大学出版社';

5、使用存在量词查询没有借阅图书的读者编号、读者名称和工作单位。

select a.读者编号,姓名,工作单位

from 读者 a

where not exists (select *

from 借阅 b where a.读者编号=b.读者编号);

6、查询至少借阅了3本图书的读者编号、读者名称、图书编号、图书名称,按读者编号排序输出。

select a.读者编号,姓名,b.图书编号,图书图书名 from 读者 a,图书 b,借阅 c

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号 and a.读者编号 in (select 读者编号 from 借阅 group by 读者编号 having count(*)>=3)

order by a.读者编号;

7、查询借书时间在2005到2006年之间的读者编号、读者名称、图书编号、图书名称。

select a.读者编号,姓名,b.图书编号,图书图书名 from 读者 a,图书 b,借阅 c

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号 and year(借阅日期) between 2005 and 2006;

8、使用存在量词查询借阅了图书的读者编号、读者名称和工作单位。

select a.读者编号,姓名,工作单位 from 读者 a

where exists (select *

from 借阅 b where a.读者编号=b.读者编号);

9、查询所借图书的总价在150元以上的读者编号、读者名称和所借图书的总价。

select a.读者编号,姓名,sum(价格)

from 读者 a,图书 b,借阅 c

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号 group by a.读者编号,姓名

having sum(价格)>=150;

10、查询没有借阅管理类图书的读者编号、读者名称、出生日期。

select 读者编号,姓名,

mid(身份证号,7,4)+'-'+mid(身份证号,11,2)+'-'+mid(身份证号,13,2) birthday from 读者

where 读者编号 not in (select a.读者编号 from 读者 a,图书 b,借阅 c,图书分类号 d

where a.读者编号=c.读者编号 and b.图书编号=c.图书编号 and b.分类号=d.分类号 and 分类号图书名='管理类');

习题9

1、将清华大学出版社出版的图书的单价提高10%。

update 图书 set 价格=价格*1.1 where publishing图书名=’清华大学出版社’; 2、将计算机应用类的图书价格减少5元。

update 图书 set 价格=价格-5

where 图书编号 in (select 分类号 from 图书分类号 where 分类号图书名=’计算机应用类’);

3、将1987年出生的读者所借图书的归还标志改为已归返。

update 借阅 set 是否归还 = .T.

where 读者编号 in (select 读者编号 from 读者 where mid(身份证号,7,4)=’1987’);

4、插入一条借书记录:读者编号06-00008,图书编号003-000024,借书日期03/25/07,还书日期05/25/07,是否归返F。

insert into 借阅 values (‘06-00008’,’003-000024’,{^2007-03-25},{^2007-05-25},.F.); 5、删除2006年8月的借书记录。

delete from 借阅 where year(借阅日期)=2006 and month(借阅日期)=8; 6、删除文学类的借书记录。

delete from 借阅 where 图书编号 in (

select a.图书编号 from 图书 a,图书分类号 b

where a.分类号=b.分类号 and 分类号图书名=’文学类’); 7、删除马永强读者的借书记录。

delete from 借阅 where 读者编号 in

(select 读者编号 from 读者 where 姓名=’马永强’);

本文来源:https://www.bwwdw.com/article/hlhv.html

Top