20260706MySQL作业题

1237 字
6 分钟
20260706MySQL作业题

图书馆管理系统DQL查询案例#

准备工作#

-- 创建数据库
drop database if exists library_db;
create database library_db;
use library_db;
-- 创建图书类别表
create table library_db.book_category (
category_id varchar(10) primary key,
category_name varchar(50) not null,
description varchar(100)
);
-- 创建作者表
create table library_db.author (
author_id int primary key auto_increment,
author_name varchar(50) not null,
nationality varchar(20),
birth_year int
);
-- 创建图书表
create table library_db.book (
book_id int primary key,
book_name varchar(100) not null,
author_id int not null,
category_id varchar(10),
publish_year int,
price decimal(10,2),
stock int default 0
);
-- 创建借阅记录表
create table library_db.borrow_record (
record_id int primary key auto_increment,
book_id int not null,
reader_name varchar(50) not null,
borrow_date date not null,
return_date date,
is_returned boolean default false
);
-- 插入图书类别数据
insert into library_db.book_category values
('c001', '文学小说', '各类文学作品和小说'),
('c002', '科学技术', '计算机、工程等科技类书籍'),
('c003', '历史地理', '历史、地理相关书籍'),
('c004', '艺术设计', '绘画、设计等艺术类书籍'),
('c005', '经济管理', '经济、商业和管理类书籍');
-- 插入作者数据
insert into library_db.author(author_name, nationality, birth_year) values
('鲁迅', '中国', 1881),
('莫言', '中国', 1955),
('J.K.罗琳', '英国', 1965),
('刘慈欣', '中国', 1963),
('余华', '中国', 1960),
('东野圭吾', '日本', 1958),
('村上春树', '日本', 1949),
('钱钟书', '中国', 1910);
-- 插入图书数据
insert into library_db.book values
(1, '百年孤独', 3, 'c001', 1967, 59.80, 15),
(2, '三体', 4, 'c002', 2006, 45.00, 20),
(3, '活着', 5, 'c001', 1993, 35.00, 10),
(4, '白夜行', 6, 'c001', 1999, 56.00, 8),
(5, '围城', 8, 'c001', 1947, 36.00, 12),
(6, '1984', 3, 'c001', 1949, 42.00, 18),
(7, '哈利波特与魔法石', 3, 'c001', 1997, 48.00, 25),
(8, '人类简史', 4, 'c003', 2011, 68.00, 15),
(9, '设计中的设计', 7, 'c004', 2000, 88.00, 5),
(10, '挪威的森林', 7, 'c001', 1987, 45.00, 12),
(11, '大数据时代', 5, 'c002', 2012, 52.00, 8),
(12, '明朝那些事儿', 2, 'c003', 2006, 48.50, 20),
(13, '经济学原理', 2, 'c005', 1998, 78.00, 10),
(14, '平凡的世界', 1, 'c001', 1986, 75.00, 15),
(15, '时间简史', 3, 'c002', 1988, 45.00, 12);
-- 插入借阅记录
insert into library_db.borrow_record(book_id, reader_name, borrow_date, return_date, is_returned) values
(2, '张明', '2023-03-15', '2023-04-10', true),
(3, '李华', '2023-03-20', null, false),
(5, '王芳', '2023-04-01', '2023-04-15', true),
(2, '赵伟', '2023-04-10', null, false),
(8, '陈静', '2023-04-05', '2023-04-25', true),
(12, '刘强', '2023-04-12', '2023-05-01', true),
(14, '孙梅', '2023-04-18', null, false),
(7, '周杰', '2023-04-20', null, false),
(4, '吴婷', '2023-03-25', '2023-04-12', true),
(1, '郑刚', '2023-04-02', null, false);

查询任务#

任务1:简单查询#

  1. 查询所有图书的详细信息
  2. 查询图书名称和价格
  3. 查询所有图书类别,并为列名设置中文别名(类别ID、类别名称、描述)
  4. 查询所有不同的作者国籍
  5. 查询所有价格超过50元的图书名称和价格,并将价格列显示为”售价”
1.1
select *
from book;
# 1.2
select book_name, price
from book;
# 1.3
select category_id as 类别ID, category_name as 类别名称, description as 描述
from book_category;
# 1.4 TODO
select author_name, nationality
from author;
# 1.5
select book_name, price as 售价
from book
where price > 50;

任务2:条件查询#

  1. 查询类别ID为’c001’的所有图书
  2. 查询价格在40元到60元之间的所有图书
  3. 查询2000年以后出版的图书
  4. 查询书名中包含”设计”的所有图书
  5. 查询作者ID为2或4的所有图书
  6. 查询库存为0或库存大于15的图书
  7. 查询未归还的借阅记录(is_returned为false)
  8. 查询2023年4月份借出的所有图书
2.1
select *
from book
where category_id = 'c001';
# 2.2
select *
from book
where price between 40 and 60;
# 2.3
select *
from book
where publish_year >= 2000;
# 2.4
select *
from book
where book_name like '%设计%';
# 2.5
select *
from book
where author_id in (2, 4);
# 2.6
select *
from book
where stock > 15
or stock = 0;
# 2.7
select *
from borrow_record
where !is_returned;
# 2.8
select *
from borrow_record
where borrow_date like '2023-04%';

任务3:排序查询#

  1. 将所有图书按照价格从高到低排序
  2. 将科幻类(假设类别ID为’c002’)图书按照出版年份从早到晚排序
  3. 将所有图书先按照类别ID排序,再按照价格从低到高排序
3.1
select *
from book
order by price desc;
# 3.2
select *
from book
where category_id = 'c002'
order by publish_year;
# 3.3
select *
from book
order by category_id, price;

任务4:聚合查询#

  1. 统计图书总数量
  2. 计算所有图书的总价值(价格×库存)
  3. 查询最贵和最便宜的图书价格
  4. 计算每个类别图书的平均价格(结果保留两位小数)
4.1
select count(1) as 总数量
from book;
# 4.2
select book_name as 名称, stock * price as 总价值
from book;
# 4.3
select max(price), min(price)
from book;
# 4.4
select category_id, round(avg(price), 2)
from book
group by category_id;

任务5:分组查询#

  1. 统计每个类别的图书数量
  2. 统计每个读者借阅的图书数量
  3. 查询不同出版年份的图书数量,只显示图书数量大于2的年份
5.1
select category_id, count(1)
from book
group by category_id;
# 5.2
select reader_name as 读者姓名, count(1) as 借阅数量
from borrow_record
group by reader_name;
# 5.3
select publish_year, count(1) as 出版数量
from book
where publish_year >= 2000
group by publish_year;

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
20260706MySQL作业题
https://kulve.tech/posts/heimamysql/20260706mysql作业题/
作者
Kulve
发布于
2026-07-06
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
Kulve
Hello, I'm Kulve.
公告
欢迎来到我的博客!这是一则示例公告。正在建设与测试此网站。
分类
标签
最新动态
站点统计
文章
17
分类
10
标签
23
总字数
33,120
运行时长
0
最后活动
0 天前
站点信息
构建平台
GitHub Actions
博客版本
Firefly v6.15.2
文章许可
CC BY-NC-SA 4.0