参考了:
原文地址:http://www.cnblogs.com/wuzhiming/p/4867370.html
创建了表并插入了一些内容,改正了里面的一些错误。
但是第11题 建触发器的题不会,原文写的也有问题。有没大佬给补充下。
题目
本题用到下面三个关系表:
CARD 借书卡: CNO 卡号,NAME 姓名,CLASS 班级
BOOKS 图书: BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数
BORROW 借书记录: CNO 借书卡号,BNO 书号,RDATE 还书日期
备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。
创建表
CREATE DATABASE book_system;
#创建借书卡表
DROP TABLE IF EXISTS `CARD`;
CREATE TABLE `CARD`
(
`CNO` INT(4) NOT NULL AUTO_INCREMENT COMMENT '卡号',
`NAME` VARCHAR(16) NOT NULL COMMENT '姓名',
`CLASS` VARCHAR(8) NOT NULL COMMENT '班级',
PRIMARY KEY (`CNO`)
) DEFAULT CHARSET 'utf8';
#创建图书表
DROP TABLE IF EXISTS `BOOKS`;
CREATE TABLE `BOOKS`
(
`BNO` INT(4) NOT NULL AUTO_INCREMENT COMMENT '书号',
`BNAME` VARCHAR(16) NOT NULL COMMENT '书名',
`AUTHOR` VARCHAR(32) NOT NULL COMMENT '作者',
`PRICE` INT(8) NOT NULL COMMENT '单价',
`QUANTITY` INT(8) NOT NULL COMMENT '库存册数',
PRIMARY KEY (`BNO`)
) DEFAULT CHARSET 'utf8';
#创建借书记录表
DROP TABLE IF EXISTS `BORROW`;
CREATE TABLE `BORROW`
(
`CNO` INT(4) NOT NULL COMMENT '借书卡号',
`BNO` VARCHAR(16) NOT NULL COMMENT '书号',
`RDATE` DATE NOT NULL COMMENT '还书日期',
PRIMARY KEY (`CNO`, `BNO`)
) DEFAULT CHARSET 'utf8';
插入数据
# 插入数据
insert into card(CNO, NAME, CLASS)
values (01, 'Tony', 'C01'),
(02, 'Jack', 'A02'),
(03, 'Lisa', 'B03');
insert into borrow(cno, bno, rdate)
VALUES (01, 111, date('2022-09-25')),
(01, 131, date('2022-09-25')),
(03, 131, date('2022-05-24')),
(03, 161, date('2022-05-24')),
(04, 151, date('2022-04-13')),
(04, 161, date('2022-04-13')),
(05, 171, date('2022-12-23')),
(06, 181, date('2022-08-26'));
insert into books(bno, bname, author, price, quantity)
VALUES (111, '水浒', 'qwe', '35', 5),
(121, '网络安全', 'asd', '40', 3),
(131, '计算方法', 'zxc', '78', 4),
(141, '计算方法习题集', 'qaz', '24', 8),
(151, '数据库技术及应用', 'wsx', '28', 6),
(161, '组合数学', 'wsx', '56', 7),
(171, '高等数学', 'edc', '99', 3),
(181, '石岩水库', 'rfv', '47', 2);
题目与答案
# 1. 写出建立BORROW表的SQL语句,要求定义主码完整性约束和引用完整性约束
CREATE TABLE `BORROW`
(
`CNO` INT(4) NOT NULL FOREIGN KEY REFERENCE CARD(`CNO`) COMMENT '借书卡号',
`BNO` VARCHAR(16) NOT NULL FOREIGN KEY REFERENCE BOOKS(`BNO`) COMMENT '书号',
`RDATE` DATE NOT NULL COMMENT '还书日期',
PRIMARY KEY (`CNO`, `BNO`)
) DEFAULT CHARSET 'utf8';
# 2. 找出借书超过5本的读者,输出借书卡号及所借图书册数
select cno, count(1)
from borrow
group by cno
having count(1) > 5;
# 3. 查询借阅了"水浒"一书的读者,输出姓名及班级
select ca.NAME, ca.CLASS
from card ca,
borrow bor,
books bok
where ca.CNO = bor.CNO
and bor.BNO = bok.BNO
and bok.BNAME = '水浒';
# 4. 查询过期未还图书,输出借阅者(卡号)、书号及还书日期
select ca.CNO, bor.BNO, bor.RDATE
from borrow bor,
card ca
where bor.RDATE < curdate();
# 5. 查询书名包括"网络"关键词的图书,输出书号、书名、作者
select bno, bname, AUTHOR
from books
where BNAME like N'%网络%';
# 6. 查询现有图书中价格最高的图书,输出书名及作者
select BNAME, AUTHOR, PRICE
from books
where PRICE = (select max(PRICE) from books);
select BNAME, AUTHOR, PRICE
from books
order by PRICE desc
limit 1;
# 7. 查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出
select borr.cno
from BORROW borr,
books bok
where borr.bno = bok.bno
and bok.bname = '计算方法'
and not exists(select *
from books bok,
BORROW bor
where bok.bname = '计算方法习题集'
and bok.bno = bor.BNO
and borr.cno = bor.cno)
order by borr.cno desc;
# 8 将"C01"班同学所借图书的还期都延长一周
UPDATE borrow bor,card ca
SET RDATE=DATE_ADD(RDATE, INTERVAL 7 day)
WHERE bor.CNO = ca.CNO
AND ca.CLASS = N'C01';
# 9. 从BOOKS表中删除当前无人借阅的图书记录
delete
from books bok
where not exists(
select bor.BNO
from borrow bor
where bor.BNO = bok.BNO);
# 10. 如果经常按书名查询图书信息,请建立合适的索引
create index IDX_BOOKS_BNAME on books (BNAME);
# 11. 在BORROW表上建立一个触发器,完成如下功能:如果读者借阅的书名是"数据库技术及应用",就将该读者的借阅记录保存在BORROW_SAVE表中(注BORROW_SAVE表结构同BORROW表)
# 12. 建立一个视图,显示"C01"班学生的借书信息(只要求显示姓名和书名)
CREATE VIEW V_VIEW
AS
SELECT a.NAME, b.BNAME
FROM BORROW ab,
CARD a,
BOOKS b
WHERE ab.CNO = a.CNO
AND ab.BNO = b.BNO
AND a.CLASS = N'C01';
DESC V_VIEW;
# 13. 查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序输出
select a.cno
from (select bor.CNO
from books bok,
borrow bor
where bor.BNO = bok.BNO
and BNAME = '计算方法') a
inner join
(select bor.CNO
from books bok,
borrow bor
where bor.BNO = bok.BNO
and BNAME = '组合数学') b on a.CNO = b.CNO
order by CNO ASC;
SELECT a.CNO
FROM BORROW a,
BOOKS b
WHERE a.BNO = b.BNO
AND b.BNAME IN (N'计算方法', N'组合数学')
GROUP BY a.CNO
HAVING COUNT(*) = 2
ORDER BY a.CNO ASC;
# 14. 假定在建BOOKS表时没有定义主码,写出为BOOKS表追加定义主码的语句
alter table books
add primary key (BNO);
# 15.1 将NAME最大列宽增加到10个字符(假定原为6个字符)
alter table card
modify column NAME varchar(30);
# 15.2 为该表增加1列NAME(系名),可变长,最大20个字符
ALTER TABLE CARD
ADD 系名 varchar(20);