20260702MySQL作业题
1266 字
6 分钟
20260702MySQL作业题
📝 综合作业:宠物收容所数据库操作
第一部分:DDL 操作(数据定义语言)
1. 创建数据库
- 删除已存在的
pet_shelter数据库(如果存在)。 - 创建名为
pet_shelter的新数据库。 - 字符集设为
utf8。 - 使用该数据库。
2. 创建三张表
表1:工作人员表(staff)
| 字段名 | 数据类型 | 说明 | |
|---|---|---|---|
| staff_id | int | 工作人员编号 | 主键 |
| full_name | varchar(30) | 姓名 | — |
| position | varchar(20) | 职位(如“兽医”“管理员”) | — |
| hire_date | date | 入职日期 | — |
| phone | varchar(15) | 联系电话 | — |
表2:宠物表(pet)
| 字段名 | 数据类型 | 说明 | |
|---|---|---|---|
| pet_id | int | 宠物编号 | 主键 |
| name | varchar(20) | 宠物名字(如“小白”“旺财”) | — |
| species | varchar(10) | 物种(“猫”或“狗”) | — |
| breed | varchar(30) | 品种(如“中华田园犬”“英短”) | — |
| intake_date | date | 入所日期 | — |
| health_status | varchar(20) | 健康状况(如“健康”“需治疗”) | — |
表3:领养记录表(adoption)
| 字段名 | 数据类型 | 说明 | |
|---|---|---|---|
| adopt_id | int | 领养记录编号 | 主键 |
| pet_id | int | 宠物编号 | — |
| adopter_name | varchar(30) | 领养人姓名 | — |
| adopt_date | date | 领养日期 | — |
| staff_id | int | 负责办理的工作人员编号 | — |
3. 修改表结构(DDL 修改操作)
请按顺序执行以下修改:
- 为
staff表添加一个新字段:email varchar(50)。 - 将
pet表中的health_status字段重命名为status,类型保持varchar(20)。 - 删除
adoption表中的staff_id字段。
第二部分:DML 操作(数据操作语言)
1. 插入数据
向 staff 表插入以下 3 条记录(包含新增的 email 字段):
| staff_id | full_name | position | hire_date | phone | |
|---|---|---|---|---|---|
| 101 | 王建国 | 管理员 | 2023-05-10 | 13811112222 | wang@shelter.org |
| 102 | 李医生 | 兽医 | 2022-11-01 | 13922223333 | li@shelter.org |
| 103 | 张小花 | 清洁员 | 2024-01-15 | 13733334444 | zhang@shelter.org |
向 pet 表插入以下 4 条记录(注意字段名已改为 status):
| pet_id | name | species | breed | intake_date | status |
|---|---|---|---|---|---|
| 1 | 小白 | 猫 | 中华田园猫 | 2025-02-01 | 健康 |
| 2 | 旺财 | 狗 | 中华田园犬 | 2025-02-05 | 需治疗 |
| 3 | 咖啡 | 猫 | 英国短毛猫 | 2025-02-10 | 健康 |
| 4 | 雪球 | 狗 | 萨摩耶 | 2025-02-12 | 健康 |
向 adoption 表插入以下 2 条记录(注意:已无 staff_id 字段):
| adopt_id | pet_id | adopter_name | adopt_date |
|---|---|---|---|
| 1001 | 1 | 刘女士 | 2025-03-01 |
| 1002 | 3 | 陈先生 | 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_idalter 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 staffset phone = '13999998888'where full_name = '李医生';
update petset status = '已康复'where name = '旺财';
update adoptionset adopter_name = '刘芳'where adopter_name = '刘女士';
# 删除数据deletefrom petwhere species = '狗' and status = '健康';
deletefrom adoptionwhere pet_id = 4;
deletefrom staffwhere full_name = '张小花';
# 清空操作deletefrom adoptionwhere adopt_id >= 0; # 条件为抑制IntelliJ安全机制truncate table pet;
# 获取表DDLshow create table staff;# 结构化输出表结构desc staff;支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
20260702MySQL作业题
https://kulve.tech/posts/heimamysql/20260702mysql作业题/




















