20260710MySQL作业题

857 字
4 分钟
20260710MySQL作业题

Day05 作业题#

第1题#

数据准备:

-- 创建数据库
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. 每页显示5条图书记录,查询第1页的数据(按book_id排序)
  2. 每页显示3条记录,查询借阅记录的第2页数据(按record_id排序)
  3. 每页显示4条记录,查询价格最高的图书的第3页(按price降序排序)

第2题 判断题#

判断下面建表语句是否有语法错误,如有请改正:

create table orders (
id int primary key auto_increment,
user_id int unsigned,
foreign key user_id references user(id)
);
create table orders (
id int primary key auto_increment,
user_id int unsigned,
# 这里缺少()
foreign key (user_id) references user(id)
);

现有两张表:

create table class
(
cid int primary key,
cname varchar(20)
);
create table student
(
sid int primary key,
sname varchar(20),
class_id int,
foreign key (class_id)
references class (cid)
on delete cascade
on update cascade
);

执行以下 SQL,结果如何?

# 级联更新,将cid=1的学生,改成cid=2
update class
set cid = 2
where cid = 1;
# 级联删除cid = 2的学生
delete
from class
where cid = 2;

第3题 选课系统#

学生表 student(sid)

课程表 course(cid)

选课关系表 student_course

要求:

  1. 防止重复选课
  2. 删除学生时自动删除选课记录
  3. 禁止删除仍有学生选修的课程

请写出完整建表 SQL。

支持与分享

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

打赏
20260710MySQL作业题
https://kulve.tech/posts/heimamysql/20260710mysql作业题/
作者
Kulve
发布于
2026-07-10
许可协议
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