MySQL 判断和删除索引和表外键

--
-- Procedure Of Delete Index
--
DROP PROCEDURE IF EXISTS DEL_IDX;
CREATE PROCEDURE DEL_IDX(IN P_TABLE_NAME varchar(200), IN P_IDX_NAME VARCHAR(200))
BEGIN
DECLARE ST VARCHAR(250);
  SET @ST=CONCAT(' DROP INDEX ', P_IDX_NAME, ' ON ', P_TABLE_NAME);
  SELECT count(*) INTO @CNT FROM information_schema.statistics WHERE table_name=P_TABLE_NAME and index_name=P_IDX_NAME;
  IF @CNT >0 THEN
    PREPARE stmt FROM @ST;
    EXECUTE stmt;
  END IF;
END;
CALL DEL_IDX('T_NAME, 'IDX_NAME');
DROP PROCEDURE IF EXISTS DEL_IDX;

--
-- Procedure Of Delete FK
--
DROP PROCEDURE IF EXISTS DEL_FK;
CREATE PROCEDURE DEL_FK(IN P_TABLE_NAME varchar(200), IN F_K_NAME VARCHAR(200))
BEGIN
DECLARE ST VARCHAR(250);
  SET @ST=CONCAT(' ALTER TABLE ', P_TABLE_NAME, ' DROP FOREIGN KEY ', F_K_NAME);
  SELECT COUNT(*) INTO @CNT FROM information_schema.key_column_usage WHERE table_name=P_TABLE_NAME and constraint_name=F_K_NAME;
  IF @CNT >0 THEN
    PREPARE stmt FROM @ST;
    EXECUTE stmt;
  END IF;
END;
CALL DEL_FK('T_NAM, 'FK_NAME);
DROP PROCEDURE IF EXISTS DEL_FK;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值