题目来源于:http://blog.csdn.net/lifetragedy/article/details/10305735
/*下面是一个基于图书系统的15道SQL问答,供大家参考
问题描述:
本题用到下面三个关系表:
T_CARD 借书卡。 CNO 卡号,NAME 姓名,CLASS 班级
T_BOOKS 图书。 BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数
T_BORROW 借书记录。 CNO 借书卡号,BNO 书号,RDATE 还书日期
备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。
要求实现如下15个处理:
*/
--1. 写出建立T_BORROW表的SQL语句,要求定义主码完整性约束和引用完整性约束。
/*
create table T_CARD
(
cno number(10) primary key,
name varchar2(100),
class varchar2(100)
);
create table T_BOOKS
(
bno number(10) primary key,
name varchar2(100),
author varchar2(100),
price number(10,2),
quantity number(10)
);
create table T_BORROW
(
cno number(10),
bno number(10),
rdate date
);
alter table T_BORROW
add constraint borrow_pk primary key (cno, bno);
alter table T_BORROW
add constraint boorow_fk_cno foreign key (cno) references T_CARD(cno);
alter table T_BORROW
add constraint boorow_fk_bno foreign key (bno) references T_BOOKS(bno);
insert into T_CARD VALUES(1, 'bryant', 'class_1');
insert into T_CARD VALUES(2, 'foxus', 'class_1');
insert into T_CARD VALUES(3, 'ennel', 'class_2');
insert into T_CARD VALUES(4, 'keliy', 'class_3');
insert into T_CARD VALUES(5, 'cinal', 'class_2');
insert into T_CARD VALUES(6, 'oopp', 'class_3');
INSERT INTO T_BOOKS VALUES(1, 'Chinese Book', 'Mr.mao', 35.8, 20);
INSERT INTO T_BOOKS VALUES(2, 'Math Book', 'Mr.xiao', 55.4, 20);
INSERT INTO T_BOOKS VALUES(3, 'English Book', 'Mr.li', 22.6, 20);
INSERT INTO T_BOOKS VALUES(4, 'Computer Book', 'Mr.yang', 78.8, 15);
INSERT INTO T_BOOKS VALUES(5, 'Music Book', 'Mr.wang', 25.3, 15);
INSERT INTO T_BOOKS VALUES(6, 'History Book', 'Mr.mao', 40.8, 12);
INSERT INTO T_BOOKS VALUES(7, 'Physics Book', 'Mr.tang', 46.6, 10);
INSERT INTO T_BOOKS VALUES(8, 'Chemistry Book', 'Mr.zou', 33.9, 10);
INSERT INTO T_BOOKS VALUES(9, 'Biology Book', 'Mr.tu', 23, 10);
INSERT INTO T_BOOKS VALUES(10, 'Political Book', 'Mr.ke', 36.2, 10);
insert into T_BORROW values(1, 1, to_date('2015-5-21', 'yyyy-MM-dd'));
insert into T_BORROW values(2, 1, to_date('2015-5-21', 'yyyy-MM-dd'));
insert into T_BORROW values(3, 3, to_date('2015-5-28', 'yyyy-MM-dd'));
insert into T_BORROW values(4, 8, to_date('2015-6-21', 'yyyy-MM-dd'));
insert into T_BORROW values(5, 4, to_date('2015-5-11', 'yyyy-MM-dd'));
insert into T_BORROW values(6, 10, to_date('2015-5-31', 'yyyy-MM-dd'));
insert into T_BORROW values(1, 9, to_date('2015-6-10', 'yyyy-MM-dd'));
insert into T_BORROW values(2, 3, to_date('2015-7-2', 'yyyy-MM-dd'));
insert into T_BORROW values(3, 5, to_date('2015-6-5', 'yyyy-MM-dd'));
insert into T_BORROW values(4, 6, to_date('2015-6-2', 'yyyy-MM-dd'));
insert into T_BORROW values(5, 9, to_date('2015-5-22', 'yyyy-MM-dd'));
insert into T_BORROW values(6, 1, to_date('2015-6-1', 'yyyy-MM-dd'));
insert into T_BORROW values(1, 3, to_date('2015-6-1', 'yyyy-MM-dd'));
insert into T_BORROW values(2, 4, to_date('2015-6-15', 'yyyy-MM-dd'));
insert into T_BORROW values(3, 8, to_date('2015-5-15', 'yyyy-MM-dd'));
insert into T_BORROW values(4, 10, to_date('2015-6-22', 'yyyy-MM-dd'));
insert into T_BORROW values(5, 7, to_date('2015-6-13', 'yyyy-MM-dd'));
insert into T_BORROW values(6, 6, to_date('2015-5-18', 'yyyy-MM-dd'));
insert into T_BORROW values(1, 8, to_date('2015-5-19', 'yyyy-MM-dd'));
insert into T_BORROW values(2, 9, to_date('2015-5-20', 'yyyy-MM-dd'));
insert into T_BORROW values(3, 6, to_date('2015-6-15', 'yyyy-MM-dd'));
insert into T_BORROW values(4, 2, to_date('2015-6-15', 'yyyy-MM-dd'));
insert into T_BORROW values(5, 2, to_date('2015-6-15', 'yyyy-MM-dd'));
调整库存数
update T_BOOKS b set b.quantity=b.quantity-(select count(*) from T_BORROW where bno=b.bno);
*/
-- 2. 找出借书超过3本的读者,输出借书卡号及所借图书册数。
select cno, count(*) cs from t_borrow br group by cno having count(*) > 3;
--3. 查询借阅了"Chinese Book"一书的读者,输出姓名及班级。
select c.cno,c.name cname, c.class classname from t_card c, t_books b, t_borrow bo where c.cno=bo.cno and b.bno=bo.bno and b.name='Chinese Book';
--4. 查询过期未还图书,输出借阅者(卡号)、书号及还书日期。
select cno, bno, rdate from t_borrow where rdate