43 lines
2.5 KiB
SQL
43 lines
2.5 KiB
SQL
-- ============================================================================
|
||
-- 教师住宿分配(复用 train_accommodation,与学生住宿逻辑隔离)
|
||
--
|
||
-- 1. 新增 occupant_type 区分入住人员类型:student 学员 / teacher 教师
|
||
-- - 存量数据全部为学员住宿,默认值 'student' 保证学生逻辑完全不变
|
||
-- - 教师住宿记录 occupant_type = 'teacher',enrollment_id 存 train_teacher.id
|
||
-- 2. 删除 fk_accomm_enrollment 外键:教师住宿的 enrollment_id 不再指向
|
||
-- train_enrollment(enrollment_id),改由业务层按 occupant_type 分别校验
|
||
-- 3. 唯一索引由「全局活唯一(enrollment_id)」改为「按人员类型隔离的活唯一
|
||
-- (occupant_type, enrollment_id)」,避免教师 id 与参训档案 id 数值碰撞
|
||
--
|
||
-- 适配数据库:Kingbase / PostgreSQL(train 模块以该语法为准)
|
||
-- ============================================================================
|
||
|
||
-- 1. 入住人员类型列
|
||
ALTER TABLE train_accommodation ADD COLUMN occupant_type VARCHAR(16) NOT NULL DEFAULT 'student';
|
||
|
||
COMMENT ON COLUMN train_accommodation.occupant_type IS '入住人员类型:student 学员 / teacher 教师';
|
||
|
||
-- 2. 解除参训档案外键约束(教师住宿记录不指向 train_enrollment)
|
||
ALTER TABLE train_accommodation DROP CONSTRAINT IF EXISTS fk_accomm_enrollment;
|
||
|
||
-- 3. 重建活跃唯一索引:按人员类型隔离
|
||
DROP INDEX IF EXISTS uq_accommodation_enroll;
|
||
|
||
CREATE UNIQUE INDEX uq_accommodation_occupant
|
||
ON train_accommodation (occupant_type, enrollment_id)
|
||
WHERE accommodation_status IN (0, 1);
|
||
|
||
CREATE INDEX IF NOT EXISTS idx_accommodation_occupant
|
||
ON train_accommodation (occupant_type, enrollment_id);
|
||
|
||
-- ============================================================================
|
||
-- MySQL 兼容说明(如需在 MySQL 环境执行,替换为下列语句):
|
||
-- ALTER TABLE train_accommodation ADD COLUMN occupant_type VARCHAR(16) NOT NULL DEFAULT 'student'
|
||
-- COMMENT '入住人员类型:student 学员 / teacher 教师';
|
||
-- ALTER TABLE train_accommodation DROP FOREIGN KEY fk_accomm_enrollment;
|
||
-- ALTER TABLE train_accommodation DROP INDEX uq_accommodation_enroll;
|
||
-- CREATE UNIQUE INDEX uq_accommodation_occupant
|
||
-- ON train_accommodation (occupant_type, enrollment_id, accommodation_status);
|
||
-- (MySQL 无部分索引,若需「仅活跃唯一」请使用生成列方案,教师模块不依赖该唯一性做业务判断)
|
||
-- ============================================================================
|