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:简单查询
- 查询所有图书的详细信息
- 查询图书名称和价格
- 查询所有图书类别,并为列名设置中文别名(类别ID、类别名称、描述)
- 查询所有不同的作者国籍
- 查询所有价格超过50元的图书名称和价格,并将价格列显示为”售价”
select *from book;
# 1.2select book_name, pricefrom book;
# 1.3select category_id as 类别ID, category_name as 类别名称, description as 描述from book_category;
# 1.4 TODOselect author_name, nationalityfrom author;
# 1.5select book_name, price as 售价from bookwhere price > 50;任务2:条件查询
- 查询类别ID为’c001’的所有图书
- 查询价格在40元到60元之间的所有图书
- 查询2000年以后出版的图书
- 查询书名中包含”设计”的所有图书
- 查询作者ID为2或4的所有图书
- 查询库存为0或库存大于15的图书
- 查询未归还的借阅记录(is_returned为false)
- 查询2023年4月份借出的所有图书
select *from bookwhere category_id = 'c001';
# 2.2select *from bookwhere price between 40 and 60;
# 2.3select *from bookwhere publish_year >= 2000;
# 2.4select *from bookwhere book_name like '%设计%';
# 2.5select *from bookwhere author_id in (2, 4);
# 2.6select *from bookwhere stock > 15 or stock = 0;
# 2.7select *from borrow_recordwhere !is_returned;
# 2.8select *from borrow_recordwhere borrow_date like '2023-04%';任务3:排序查询
- 将所有图书按照价格从高到低排序
- 将科幻类(假设类别ID为’c002’)图书按照出版年份从早到晚排序
- 将所有图书先按照类别ID排序,再按照价格从低到高排序
select *from bookorder by price desc;
# 3.2select *from bookwhere category_id = 'c002'order by publish_year;
# 3.3select *from bookorder by category_id, price;任务4:聚合查询
- 统计图书总数量
- 计算所有图书的总价值(价格×库存)
- 查询最贵和最便宜的图书价格
- 计算每个类别图书的平均价格(结果保留两位小数)
select count(1) as 总数量from book;
# 4.2select book_name as 名称, stock * price as 总价值from book;
# 4.3select max(price), min(price)from book;
# 4.4select category_id, round(avg(price), 2)from bookgroup by category_id;任务5:分组查询
- 统计每个类别的图书数量
- 统计每个读者借阅的图书数量
- 查询不同出版年份的图书数量,只显示图书数量大于2的年份
select category_id, count(1)from bookgroup by category_id;
# 5.2select reader_name as 读者姓名, count(1) as 借阅数量from borrow_recordgroup by reader_name;
# 5.3select publish_year, count(1) as 出版数量from bookwhere publish_year >= 2000group by publish_year;支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
20260706MySQL作业题
https://kulve.tech/posts/heimamysql/20260706mysql作业题/




















