guoyu/log/删除编号200以后的学生.sql
2025-12-11 23:28:07 +08:00

161 lines
4.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ========================================
-- 删除编号200以后的学生及其所有相关数据
-- ========================================
-- 账号root
-- 密码root
-- 数据库study
-- ========================================
-- ========================================
-- 第1步先查询要删除的学生确认
-- ========================================
-- 查询编号200以后的学生用户
SELECT
user_id,
user_name,
nick_name,
user_type,
create_time
FROM sys_user
WHERE user_id > 200
AND user_type IN ('02', 'student')
ORDER BY user_id;
-- 统计数量
SELECT COUNT(*) AS will_delete_count
FROM sys_user
WHERE user_id > 200
AND user_type IN ('02', 'student');
-- ========================================
-- 第2步删除相关数据按依赖顺序
-- ========================================
-- 2.1 删除学习记录详情
DELETE FROM study_learning_detail
WHERE student_id > 200;
-- 2.2 删除学习记录
DELETE FROM study_learning_record
WHERE student_id > 200;
-- 2.3 删除课程分配
DELETE FROM study_course_assignment
WHERE student_id > 200;
-- 2.4 删除学生班级关联(重要:这是导致数量不一致的主要原因)
DELETE FROM study_student_class
WHERE student_id > 200;
-- 2.5 删除考试记录
DELETE FROM study_exam_record
WHERE student_id > 200;
-- 2.6 删除答题记录
DELETE FROM study_student_answer
WHERE student_id > 200;
-- 2.7 删除学生变更记录
DELETE FROM study_student_change_log
WHERE student_id > 200;
-- 2.8 删除语音评测记录(如果有)
DELETE FROM study_voice_evaluation
WHERE student_id > 200;
-- ========================================
-- 第3步删除用户角色和岗位关联
-- ========================================
-- 3.1 删除用户角色关联
DELETE FROM sys_user_role
WHERE user_id > 200
AND user_id IN (
SELECT user_id FROM sys_user
WHERE user_type IN ('02', 'student')
);
-- 3.2 删除用户岗位关联
DELETE FROM sys_user_post
WHERE user_id > 200
AND user_id IN (
SELECT user_id FROM sys_user
WHERE user_type IN ('02', 'student')
);
-- ========================================
-- 第4步最后删除用户表记录
-- ========================================
DELETE FROM sys_user
WHERE user_id > 200
AND user_type IN ('02', 'student');
-- ========================================
-- 第5步验证删除结果
-- ========================================
-- 验证1确认编号200以后的学生已删除
SELECT COUNT(*) AS remaining_students_after_200
FROM sys_user
WHERE user_id > 200
AND user_type IN ('02', 'student');
-- 应该返回 0
-- 验证2查看剩余的学生数量
SELECT
'用户表学生数' AS type,
COUNT(*) AS count
FROM sys_user
WHERE user_type IN ('02', 'student')
UNION ALL
SELECT
'班级学生数' AS type,
COUNT(DISTINCT student_id) AS count
FROM study_student_class
UNION ALL
SELECT
'课程分配学生数' AS type,
COUNT(DISTINCT student_id) AS count
FROM study_course_assignment;
-- 三个数字应该一致
-- 验证3查看剩余学生的编号范围
SELECT
MIN(user_id) AS min_id,
MAX(user_id) AS max_id,
COUNT(*) AS total_count
FROM sys_user
WHERE user_type IN ('02', 'student');
-- max_id 应该 <= 200
-- 验证4确认没有孤儿数据
SELECT
'孤儿班级关联' AS type,
COUNT(*) AS count
FROM study_student_class sc
LEFT JOIN sys_user u ON sc.student_id = u.user_id
WHERE u.user_id IS NULL
UNION ALL
SELECT
'孤儿课程分配' AS type,
COUNT(*) AS count
FROM study_course_assignment ca
LEFT JOIN sys_user u ON ca.student_id = u.user_id
WHERE u.user_id IS NULL
UNION ALL
SELECT
'孤儿学习记录' AS type,
COUNT(*) AS count
FROM study_learning_record lr
LEFT JOIN sys_user u ON lr.student_id = u.user_id
WHERE u.user_id IS NULL;
-- 所有应该返回 0
-- ========================================
-- 完成!
-- ========================================
-- 删除成功后,用户管理、课程分配、班级管理的学生数量应该一致
-- ========================================