20260702MySQL作业题

1266 字
6 分钟
20260702MySQL作业题

📝 综合作业:宠物收容所数据库操作#

第一部分:DDL 操作(数据定义语言)#

1. 创建数据库#

  • 删除已存在的 pet_shelter 数据库(如果存在)。
  • 创建名为 pet_shelter 的新数据库。
  • 字符集设为 utf8
  • 使用该数据库。

2. 创建三张表#

表1:工作人员表(staff#

字段名数据类型说明
staff_idint工作人员编号主键
full_namevarchar(30)姓名
positionvarchar(20)职位(如“兽医”“管理员”)
hire_datedate入职日期
phonevarchar(15)联系电话

表2:宠物表(pet#

字段名数据类型说明
pet_idint宠物编号主键
namevarchar(20)宠物名字(如“小白”“旺财”)
speciesvarchar(10)物种(“猫”或“狗”)
breedvarchar(30)品种(如“中华田园犬”“英短”)
intake_datedate入所日期
health_statusvarchar(20)健康状况(如“健康”“需治疗”)

表3:领养记录表(adoption#

字段名数据类型说明
adopt_idint领养记录编号主键
pet_idint宠物编号
adopter_namevarchar(30)领养人姓名
adopt_datedate领养日期
staff_idint负责办理的工作人员编号

3. 修改表结构(DDL 修改操作)#

请按顺序执行以下修改:

  1. staff 表添加一个新字段:email varchar(50)
  2. pet 表中的 health_status 字段重命名为 status,类型保持 varchar(20)
  3. 删除 adoption 表中的 staff_id 字段。

第二部分:DML 操作(数据操作语言)#

1. 插入数据#

staff 表插入以下 3 条记录(包含新增的 email 字段):#

staff_idfull_namepositionhire_datephoneemail
101王建国管理员2023-05-1013811112222wang@shelter.org
102李医生兽医2022-11-0113922223333li@shelter.org
103张小花清洁员2024-01-1513733334444zhang@shelter.org

pet 表插入以下 4 条记录(注意字段名已改为 status):#

pet_idnamespeciesbreedintake_datestatus
1小白中华田园猫2025-02-01健康
2旺财中华田园犬2025-02-05需治疗
3咖啡英国短毛猫2025-02-10健康
4雪球萨摩耶2025-02-12健康

adoption 表插入以下 2 条记录(注意:已无 staff_id 字段):#

adopt_idpet_idadopter_nameadopt_date
10011刘女士2025-03-01
10023陈先生2025-03-05

2. 更新数据(UPDATE)#

  • 将工作人员 李医生 的电话更新为 13999998888
  • 将宠物 旺财status 改为 "已康复"
  • 将领养人 刘女士 的姓名更新为 "刘芳"

3. 删除数据(DELETE)#

  • 删除所有 species 为 ‘狗’ 且 status 为 ‘健康’ 的宠物记录。
  • 删除 pet_id = 4 的领养记录(如果存在)。
  • 删除工作人员 张小花 的记录。

4. 清空操作#

  • 使用 DELETE 清空 adoption 表。
  • 使用 TRUNCATE 清空 pet 表。

答案#

# 1. 创建数据库
# 删除已存在的pet_shelter数据库
drop database if exists pet_shelter;
# 新建pet_shelter数据库
create database if not exists pet_shelter
character set utf8mb3;
show create database pet_shelter;
use pet_shelter;
# 2. 创建3张表
# 创建工作人员表(staff)
create table if not exists staff
(
# 主键不要添加not null,这会导致无法使用null占位符
staff_id int primary key auto_increment comment '工作人员编号',
full_name varchar(30) not null comment '姓名',
position varchar(30) not null comment '职位',
hire_date date not null default (current_date) comment '入职日期',
phone varchar(15) not null comment '联系电话'
) comment '工作人员表';
# 创建宠物表(pet)
create table if not exists pet
(
pet_id int primary key auto_increment comment '宠物编号',
name varchar(20) not null comment '宠物名字',
species varchar(10) not null comment '物种',
breed varchar(30) not null comment '品种',
intake_date date not null default (current_date) comment '入所日期',
health_status varchar(20) not null comment '健康状况'
) comment '宠物表';
# 创建领养记录表(adoption)
create table if not exists adoption
(
adopt_id int primary key auto_increment comment '领养记录编号',
pet_id int not null comment '宠物编号',
adopter_name varchar(30) not null comment '领养人姓名',
adopt_date date not null default (current_date) comment '领养日期',
staff_id int not null comment '工作人员编号'
) comment '领养记录表';
# 修改表结构DDL操作
# 新增邮箱地址字段
alter table staff
add column email varchar(50) comment '邮箱地址';
# 修改列名 health_status -> status: 方式一: change column <源列名> <目标列名> <列属性定义>
alter table pet
change column health_status status varchar(20) not null comment '健康状况';
# 修改列名 health_status -> status: 方式二: rename column <源列名> to <目标列名>
alter table pet
rename column health_status to status;
# 删除列staff_id
alter table adoption
drop column staff_id;
# 第二部分: DML操作
insert into staff (staff_id, full_name, position, hire_date, phone, email)
values (101, '王建国', '管理员', '2023-05-10', '13811112222', 'wang@shelter.org'),
(102, '李医生', '兽医', '2022-11-01', '13922223333', 'li@shelter.org'),
(103, '张小花', '清洁员', '2024-01-15', '13733334444', 'zhang@shelter.org');
insert into pet (pet_id, name, species, breed, intake_date, status)
values (1, '小白', '猫', '中华田园猫', '2025-02-01', '健康'),
(2, '旺财', '狗', '中华田园犬', '2025-02-05', '需治疗'),
(3, '咖啡', '猫', '英国短毛猫', '2025-02-10', '健康'),
(4, '雪球', '狗', '萨摩耶', '2025-02-12', '健康');
insert into adoption (adopt_id, pet_id, adopter_name, adopt_date)
values (1001, 1, '刘女士', '2025-03-01'),
(1002, 3, '陈先生', '2025-03-05')
;
# 更新数据
update staff
set phone = '13999998888'
where full_name = '李医生';
update pet
set status = '已康复'
where name = '旺财';
update adoption
set adopter_name = '刘芳'
where adopter_name = '刘女士';
# 删除数据
delete
from pet
where species = '狗'
and status = '健康';
delete
from adoption
where pet_id = 4;
delete
from staff
where full_name = '张小花';
# 清空操作
delete
from adoption
where adopt_id >= 0; # 条件为抑制IntelliJ安全机制
truncate table pet;
# 获取表DDL
show create table staff;
# 结构化输出表结构
desc staff;

支持与分享

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

打赏
20260702MySQL作业题
https://kulve.tech/posts/heimamysql/20260702mysql作业题/
作者
Kulve
发布于
2026-06-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