-- ==========================================
-- 从 member_driver 表删除 car_number 字段
-- ==========================================
-- 说明：
-- 1. 司机和车辆是多对多关系，应该由独立的 vehicle 表管理
-- 2. 删除前请确保已经将车辆信息迁移到 vehicle 表
-- 3. 此操作不可逆，请先备份数据库
-- ==========================================

-- 步骤 1: 检查 member_driver 表结构
SELECT '步骤1: 检查 member_driver 表结构' as '执行步骤';
DESC member_driver;

-- 步骤 2: 检查字段是否存在
SELECT '步骤2: 检查 car_number 字段是否存在' as '执行步骤';
SELECT COUNT(*) as 'car_number字段存在数量' 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
  AND TABLE_NAME = 'member_driver' 
  AND COLUMN_NAME = 'car_number';

-- 步骤 3: 检查是否有触发器引用 car_number
SELECT '步骤3: 检查触发器' as '执行步骤';
SHOW TRIGGERS WHERE `Table` = 'member_driver';

-- 步骤 4: 检查是否有视图引用 car_number
SELECT '步骤4: 检查视图' as '执行步骤';
SELECT TABLE_NAME, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = DATABASE()
  AND VIEW_DEFINITION LIKE '%car_number%';

-- 步骤 5: 检查delivery表的触发器（重点检查）
SELECT '步骤5: 检查 delivery 表触发器' as '执行步骤';
SHOW TRIGGERS WHERE `Table` = 'delivery';

-- 步骤 6: 查看完整的触发器定义
SELECT '步骤6: 查看所有触发器的完整定义' as '执行步骤';
SELECT 
    TRIGGER_NAME as '触发器名称',
    EVENT_MANIPULATION as '触发事件',
    EVENT_OBJECT_TABLE as '关联表',
    ACTION_STATEMENT as '触发器SQL'
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA = DATABASE()
  AND ACTION_STATEMENT LIKE '%car_number%';

-- ==========================================
-- ⚠️ 重要：执行删除前请先检查上面的输出
-- ==========================================
-- 如果确认：
-- 1. car_number 字段存在
-- 2. 没有触发器或视图引用它
-- 3. 已备份数据库
-- 则取消下面的注释执行删除操作：

-- ALTER TABLE member_driver DROP COLUMN IF EXISTS car_number;

-- ==========================================
-- 验证删除结果
-- ==========================================
-- 取消下面的注释验证字段已删除：
-- SELECT '验证: car_number 字段已删除' as '执行步骤';
-- DESC member_driver;
-- SELECT COUNT(*) as 'car_number字段剩余数量' 
-- FROM INFORMATION_SCHEMA.COLUMNS 
-- WHERE TABLE_SCHEMA = DATABASE() 
--   AND TABLE_NAME = 'member_driver' 
--   AND COLUMN_NAME = 'car_number';

-- ==========================================
-- 说明：执行完成后请：
-- 1. 清理后端 target 目录
-- 2. 重新编译后端服务
-- 3. 完全重启后端服务
-- ==========================================

