-- 管理员端“教务管理-课程管理”增量数据库脚本(MySQL 5.7+/8.0)。 -- -- 执行顺序:先执行class_management.sql补齐班级培训日期等字段,再执行本脚本。 -- 设计说明: -- 1. 项目已存在class_subject表,本脚本只调整课时精度、补充审计列和常用索引。 -- 2. 旧数据允许审计时间为空;新增/编辑接口会自动维护create_time和update_time。 -- 3. 项目历史表主要由服务层维护关系完整性,本脚本不额外引入外键,避免阻断旧数据迁移。 delimiter $$ drop procedure if exists migrate_course_management$$ create procedure migrate_course_management() begin -- 页面允许0.5课时,原int列会截断小数,因此统一调整为一位小数。 if exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'subject_hour' ) then alter table class_subject modify column subject_hour decimal(5,1) null comment '课时数,支持0.5课时粒度'; end if; if not exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'create_time' ) then alter table class_subject add column create_time datetime null comment '创建时间' after type; end if; if not exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'update_time' ) then alter table class_subject add column update_time datetime null comment '更新时间' after create_time; end if; -- 班级课程表按class_id和date过滤、排序,该索引同时服务课程导出和日期范围查询。 if not exists ( select 1 from information_schema.statistics where table_schema = database() and table_name = 'class_subject' and index_name = 'idx_subject_class_date' ) then alter table class_subject add index idx_subject_class_date (class_id, date, id); end if; -- 教师冲突校验和教师日历按teacher_id、date缩小候选集合。 if not exists ( select 1 from information_schema.statistics where table_schema = database() and table_name = 'class_subject' and index_name = 'idx_subject_teacher_date' ) then alter table class_subject add index idx_subject_teacher_date (teacher_id, date, id); end if; end$$ call migrate_course_management()$$ drop procedure if exists migrate_course_management$$ delimiter ; -- 旧记录没有可靠的原始创建时间,使用当前时间仅回填update_time,create_time保持NULL表示未知。 update class_subject set update_time = coalesce(update_time, now()) where update_time is null;