ex06数据更新操作参考答案
时间:2025-07-14
时间:2025-07-14
实验六 数据更新操作
1 实验目的与要求
(1) 掌握基本表的INSERT、UPDATE、DELETE操作。
(2) 正确理解更新操作中涉及到的相关约束问题。
2 实验内容
根据BookDB中4张关系表,完成以下更新操作:
(1) 分别给这4张表添加信息,要求图书分类表、图书表、读者表各插入5个元组,借阅表插入20个元组。
(2) 将联合股份有限公司的读者工作单位修改为联合立华股份有限公司。
(3) 将入库数量最多的图书单价下调5%。
(4) 将“经济类”的图书单价提高10%。
(5) 将借阅次数高于2次的图书数量增加50%。
(6) 将富士康科技集团读者的借书期限延长至3个月。
(7) 根据借阅表修改每个读者的借书数量。
(8) 删除价格超过50元的图书借阅信息。
(9) 删除借阅了大学英语的借阅记录。
(10) 删除从未借过书的读者。
参考答案
根据BookDb中四张关系表,完成以下更新操作:
(1) 分别给这四张表添加信息,要求图书分类表、图书表、读者表各插入5个元组,借阅表插入20个元组。插入信息见实验四
(2 将联合股份有限公司的读者工作单位修改为联合立华股份有限公司。
update reader
set workunit='联合立华股份有限公司'
from reader
where workunit='联合股份有限公司'
(3) 将入库数量最多的图书单价下调5%。
update book
set price=price*0.95
from book
where shopnum=(select max(shopnum) from book)
(4) 将“经济管理类”的图书单价提高10%。
update book
set price=price*1.1
from book a,bookclass b
where a.classno=b.classno and b.classname='经济类'
(5) 将借阅次数高于次的图书数量增加50%。
update book
set shopnum=shopnum*1.5
from book
where bookno in(select distinct bookno
from borrow
group by bookno
having count(*)>2)
(6)将富士康科技集团读者的借书期限延长至3个月。
update borrow
set shoulddate=borrowdate+90
from borrow
where readerno=(select readerno
from reader
where workunit like '富士康%')
(7) 根据借阅表修改每个读者的借书数量。
update reader
set borrowcount=borrownum
from reader a,(select readerno,count(*) borrownum
from borrow
group by readerno) b
where a.readerno=b.readerno
(8) 删除价格超过50元的图书借阅信息。
delete from borrow
where bookno in (select bookno
from book
where price>50 )
(9) 删除借阅了大学英语的借阅记录。
delete from borrow
where bookno in (select a.bookno
from borrow a,book b
where a.bookno=b.bookno and bookname like '大学英语%')
(10) 删除从未借过书的读者。
delete from reader
where readerno in(select a.readerno
from reader a left outer join borrow b
on a.readerno=b.readerno
where bookno is null)