本文是在Cat Qi的原贴的基础之上,经本人逐题分别在MySql数据库中实现的笔记.
参考原贴:http://www.cnblogs.com/qixuejia/p/3637735.html
01 问题描述
本题用到下面三个关系表:
借书卡:card
cno | name | class |
卡号 | 姓名 | 班级 |
图书:books
bno | bname | author | price | quantity |
书号 | 书名 | 作者 | 单价 | 库存册数 |
借书记录:borrow
cno | bno | rdate |
借书卡号 | 书号 | 还书日期 |
备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。
02 问题及SQL实现
1、写出建立card,books,borrow表的SQL语句,要求定义表borrow的主码完整性约束和引用完整性约束
1 drop table if exists books; 2 create table books ( 3 bno int(11) auto_increment , 4 bname varchar(30) default null, 5 author varchar(30) default null, 6 price float(6,2) default null, 7 quantity int(11) default null, 8 primary key (bno) 9 ) engine=innodb default charset=utf8; 10 11 drop table if exists card; 12 create table card ( 13 cno int(11) auto_increment, 14 name varchar(30) default null, 15 class varchar(30) default null, 16 primary key (cno) 17 ) engine=innodb default charset=utf8; 18 19 drop table if exists borrow; 20 create table borrow ( 21 cno int(11) not null, 22 bno int(11) not null, 23 rdate datetime null default current_timestamp, 24 primary key (cno,bno), 25 key bno (bno), 26 constraint bno foreign key (bno) references books (bno), 27 constraint cno foreign key (cno) references card (cno) 28 ) engine=innodb default charset=utf8;
插入测试数据:
1 insert into card(name,class) values('张三','计科一班'); 2 insert into card(name,class) values('李四','计科一班'); 3 insert into card(name,class) values('王五','计科二班'); 4 insert into card(name,class) values('六四','计科二班'); 5 insert into card(name,class) values('七七','软工一班'); 6 insert into card(name,class) values('粑粑','软工二班'); 7 8 insert into books(bname,author,price,quantity) values ('水浒','施耐庵',188,3); 9 insert into books(bname,author,price,quantity) values ('计算机网络','谢希仁',49,3); 10 insert into books(bname,author,price,quantity) values ('计算方法','严蔚敏',58,3); 11 insert into books(bname,author,price,quantity) values ('计算方法习题集','殷人昆',188,3); 12 insert into books(bname,author,price,quantity) values ('数据库技术及应用','王珊',38,3); 13 insert into books(bname,author,price,quantity) values ('组合数学','周伟',28,3); 14 insert into books(bname,author,price,quantity) values ('redis初探','周成龙',25,3); 15 16 insert into borrow(cno,bno) values(1,1); 17 insert into borrow(cno,bno) values(2,1); 18 insert into borrow(cno,bno) values(3,1); 19 insert into borrow(cno,bno) values(4,3); 20 insert into borrow(cno,bno) values(4,6); 21 insert into borrow(cno,bno) values(5,6); 22 insert into borrow(cno,bno) values(2,7);
2、找出借书多于1本的读者,输出借书卡号及所借图书册数
select cno as "卡号",count(*) as "借书册数" from borrow group by cno having count(cno)>1;
3、查询借阅了"水浒"一书的读者,输出姓名及班级
select c.name as "姓名",c.class as "班级" from card c where c.cno in ( select br.cno from borrow br join books bs on br.bno = bs.bno and bs.bname="水浒" );
4、查询过期未还图书,输出借阅者(卡号)、书号及还书日期
select cno as "卡号",bno as "书号",rdate as "还书日期" from borrow where rdate< now();
5、查询书名包括"网络"关键词的图书,输出书号、书名、作者
select bno,bname,author from books where bname like "%网络%";
6、查询现有图书中价格最高的图书,输出书名及作者
select bname,author from books where price = (select max(price) from books);
7、查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出
select bo.cno from borrow bo, books where bo.bno = books.bno and books.bname ="计算方法" and not exists ( select * from borrow r,books b where r.bno = b.bno and b.bname="计算方法习题集" ) order by bo.cno desc;
8、将"计科一班"班同学所借图书的还期都延长一周
update borrow set rdate=adddate(rdate,INTERVAL 7 day) where cno in (select cno from card where class="计科一班");
9、从books表中删除当前无人借阅的图书记录
delete from books where bno not in (select distinct bno from borrow );
这里有3本图书被删除,只剩下1、3、6、7,执行删除操作后重新插入books表的原始数据以便做后面的题目。
10、如果经常按书名查询图书信息,请建立合适的索引
create index idx_books_name on books(bname); --关于索引的使用参考: --https://zhidao.baidu.com/question/492877300.html
11、在borrow表上建立一个触发器,完成如下功能:如果读者借阅的书名是"数据库技术及应用",就将该读者的借阅记录保存在borrow_save表中(注borrow_save表结构同borrow表)
create view v_borrow as select c.name,b.bname from card c,books b,borrow r where c.cno=r.cno and b.bno=r.bno and c.class="计科一班";
12、查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序输出
select b.cno from borrow b where b.bno in (select bno from books where bname in ("计算方法","组合数学")) group by b.cno having count(b.bno)=2 order by b.cno ;
13、假定在建books表时没有定义主键,写出为books表追加定义主键的语句
alter table books add primary key (bno);
14、为card表增加1列deptname(系名),可变长,最大50个字符
alter table card add deptname varchar(50);
THE END.