-- Alter existing biz_rule to product/country/customer dimension storage SET NAMES utf8mb4; -- add new columns if missing SET @sql := ( SELECT IF( EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'biz_rule' AND column_name = 'product_code'), 'SELECT 1', 'ALTER TABLE biz_rule ADD COLUMN product_code varchar(64) DEFAULT NULL COMMENT ''产品代码(空=全部)'' AFTER rule_type' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @sql := ( SELECT IF( EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'biz_rule' AND column_name = 'country_code'), 'SELECT 1', 'ALTER TABLE biz_rule ADD COLUMN country_code varchar(10) DEFAULT NULL COMMENT ''国家ISO2(空=全部)'' AFTER product_code' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @sql := ( SELECT IF( EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'biz_rule' AND column_name = 'customer_code'), 'SELECT 1', 'ALTER TABLE biz_rule ADD COLUMN customer_code varchar(64) DEFAULT NULL COMMENT ''客户代码(空=全部)'' AFTER country_code' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- migrate CSV rows: take first token only as best-effort (manual fix may be needed) UPDATE biz_rule SET product_code = NULLIF(TRIM(SUBSTRING_INDEX(REPLACE(IFNULL(product_codes,''), ' ', ''), ',', 1)), ''), country_code = NULLIF(TRIM(SUBSTRING_INDEX(REPLACE(IFNULL(country_codes,''), ' ', ''), ',', 1)), ''), customer_code = NULLIF(TRIM(SUBSTRING_INDEX(REPLACE(IFNULL(customer_codes,''), ' ', ''), ',', 1)), '') WHERE product_code IS NULL AND EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'biz_rule' AND column_name = 'product_codes'); -- drop old CSV columns if present SET @sql := ( SELECT IF( EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'biz_rule' AND column_name = 'product_codes'), 'ALTER TABLE biz_rule DROP COLUMN product_codes, DROP COLUMN country_codes, DROP COLUMN customer_codes', 'SELECT 1' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @sql := ( SELECT IF( EXISTS(SELECT 1 FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = 'biz_rule' AND index_name = 'idx_biz_rule_dim'), 'SELECT 1', 'ALTER TABLE biz_rule ADD KEY idx_biz_rule_dim (product_code, country_code, customer_code)' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;