-- 强制重建 mes_moentry 和 mes_morder 表的主键
-- 注意：此操作会删除现有主键并重建，请确保已备份数据

USE aidopdev;

-- ========================================
-- 处理 mes_moentry 表
-- ========================================

-- 1. 删除 mes_moentry 所有PRIMARY KEY约束
ALTER TABLE mes_moentry DROP PRIMARY KEY;

-- 2. 确保 Id 字段为 NOT NULL
ALTER TABLE mes_moentry MODIFY COLUMN Id BIGINT NOT NULL AUTO_INCREMENT;

-- 3. 重新添加主键（只用 Id 字段）
ALTER TABLE mes_moentry ADD PRIMARY KEY (Id);

-- ========================================
-- 处理 mes_morder 表
-- ========================================

-- 4. 删除 mes_morder 所有PRIMARY KEY约束
ALTER TABLE mes_morder DROP PRIMARY KEY;

-- 5. 确保 Id 字段为 NOT NULL  
ALTER TABLE mes_morder MODIFY COLUMN Id BIGINT NOT NULL AUTO_INCREMENT;

-- 6. 重新添加主键（只用 Id 字段）
ALTER TABLE mes_morder ADD PRIMARY KEY (Id);

-- 验证结果
SELECT 'mes_moentry primary key columns:' AS info;
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = 'aidopdev' 
  AND TABLE_NAME = 'mes_moentry'
  AND CONSTRAINT_NAME = 'PRIMARY';

SELECT 'mes_morder primary key columns:' AS info;
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = 'aidopdev' 
  AND TABLE_NAME = 'mes_morder'
  AND CONSTRAINT_NAME = 'PRIMARY';

SELECT 'Primary keys have been rebuilt successfully' AS result;
