帮我的宿舍管理系统设计触发器(字段由你取),要10条,用数据库的知识写,要给出代码
时间: 2024-03-12 07:48:39 浏览: 54
数据库—宿舍管理系统代码
好的,根据你的要求,我可以为你设计一个包含以下10条触发器的宿舍管理系统:
1. 学生信息修改触发器:当学生的联系方式或班级信息被修改时,自动更新住宿申请记录中该学生的联系方式或班级信息。
代码实现:
```
CREATE TRIGGER update_application
AFTER UPDATE ON student
FOR EACH ROW
BEGIN
IF NEW.phone != OLD.phone OR NEW.class != OLD.class THEN
UPDATE application SET phone = NEW.phone, class = NEW.class WHERE student_id = NEW.id;
END IF;
END;
```
2. 宿舍房间入住触发器:当学生的入住申请被审核通过时,自动更新宿舍房间的住户信息和可住人数。
代码实现:
```
CREATE TRIGGER update_room_occupancy
AFTER UPDATE ON admission
FOR EACH ROW
BEGIN
IF NEW.status = 'approved' THEN
UPDATE room SET current_occupancy = current_occupancy + 1 WHERE id = NEW.room_id;
UPDATE room SET current_tenant = NEW.student_id WHERE id = NEW.room_id;
END IF;
END;
```
3. 宿舍房间退房触发器:当学生的退房申请被审核通过时,自动更新宿舍房间的住户信息和可住人数。
代码实现:
```
CREATE TRIGGER update_room_vacancy
AFTER UPDATE ON admission
FOR EACH ROW
BEGIN
IF NEW.status = 'checked out' THEN
UPDATE room SET current_occupancy = current_occupancy - 1 WHERE id = OLD.room_id;
UPDATE room SET current_tenant = NULL WHERE id = OLD.room_id;
END IF;
END;
```
4. 维修申报触发器:当学生提交宿舍维修申报时,自动向维修记录表中插入一条新记录。
代码实现:
```
CREATE TRIGGER add_repair_record
AFTER INSERT ON maintenance_request
FOR EACH ROW
BEGIN
INSERT INTO repair_record (room_id, request_id, request_date) VALUES (NEW.room_id, NEW.id, NEW.request_date);
END;
```
5. 维修处理触发器:当宿舍维修申报被处理时,自动更新维修记录表中该记录的处理状态和处理人员。
代码实现:
```
CREATE TRIGGER update_repair_record
AFTER UPDATE ON maintenance_request
FOR EACH ROW
BEGIN
IF NEW.status = 'completed' THEN
UPDATE repair_record SET status = 'completed', repairman_id = NEW.repairman_id WHERE request_id = NEW.id;
END IF;
END;
```
6. 清洁记录触发器:当宿舍清洁完成时,自动向清洁记录表中插入一条新记录。
代码实现:
```
CREATE TRIGGER add_cleaning_record
AFTER INSERT ON cleaning_schedule
FOR EACH ROW
BEGIN
INSERT INTO cleaning_record (room_id, cleaning_date) VALUES (NEW.room_id, NEW.cleaning_date);
END;
```
7. 用电记录触发器:当宿舍用电时,自动向用电记录表中插入一条新记录。
代码实现:
```
CREATE TRIGGER add_electricity_record
AFTER INSERT ON electricity_usage
FOR EACH ROW
BEGIN
INSERT INTO electricity_record (room_id, usage_date, usage_hours, usage_amount) VALUES (NEW.room_id, NEW.usage_date, NEW.usage_hours, NEW.usage_amount);
END;
```
8. 入住记录触发器:当学生入住宿舍时,自动向入住记录表中插入一条新记录。
代码实现:
```
CREATE TRIGGER add_checkin_record
AFTER INSERT ON admission
FOR EACH ROW
BEGIN
INSERT INTO checkin_record (room_id, tenant_id, checkin_date, expected_checkout_date) VALUES (NEW.room_id, NEW.student_id, NEW.checkin_date, NEW.expected_checkout_date);
END;
```
9. 退房记录触发器:当学生退房时,自动向退房记录表中插入一条新记录。
代码实现:
```
CREATE TRIGGER add_checkout_record
AFTER UPDATE ON admission
FOR EACH ROW
BEGIN
IF NEW.status = 'checked out' THEN
INSERT INTO checkout_record (room_id, tenant_id, checkout_date) VALUES (OLD.room_id, OLD.student_id, CURRENT_DATE);
END IF;
END;
```
10. 重置密码触发器:当学生忘记密码并重置密码时,自动向密码记录表中插入一条新记录。
代码实现:
```
CREATE TRIGGER add_password_reset_record
AFTER UPDATE ON student
FOR EACH ROW
BEGIN
IF NEW.password <> OLD.password THEN
INSERT INTO password_record (student_id, reset_date) VALUES (NEW.id, CURRENT_DATE);
END IF;
END;
```
以上触发器可以存储在数据库中,可以使用SQL语句进行创建和删除操作。希望这个设计能够帮到你!
阅读全文