-- 지정판매소 ↔ 판매 대행소 FK (통계·판매대장 필터용)
-- mysql --default-character-set=utf8mb4 -u ... -p DBNAME < writable/database/designated_shop_ds_sa_idx_apply.sql

SET NAMES utf8mb4;

SET @db = DATABASE();

SET @s = (SELECT IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'designated_shop' AND COLUMN_NAME = 'ds_sa_idx') > 0,
  'SELECT 1',
  'ALTER TABLE `designated_shop`
     ADD COLUMN `ds_sa_idx` INT UNSIGNED NULL DEFAULT NULL COMMENT ''소속 판매 대행소 FK'' AFTER `ds_lg_idx`,
     ADD KEY `idx_ds_sa_idx` (`ds_sa_idx`)'
));
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;
