码迷,mamicode.com
首页 > 数据库 > 详细

mysql判断表字段或索引是否存在,然后修改

时间:2019-06-16 13:29:45      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:hot   判断   The   base   com   ati   stat   form   orm   

判断字段是否存在:

DROP PROCEDURE IF EXISTS schema_change;  
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN 
DECLARE  CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name = rtc_order AND column_name = IfUpSend) THEN  
    ALTER TABLE rtc_order
    ADD COLUMN `IfUpSend` BIT  NOT NULL  DEFAULT 0 COMMENT 是否上传 是否上传;
END IF;  
END//  
DELIMITER ;  
CALL schema_change();

 

判断索引是否存在:

DROP PROCEDURE IF EXISTS schema_change;  
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN 
DECLARE  CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = rtc_phototype AND index_name = index_name) THEN  
   ALTER TABLE `rtc_Phototype` ADD INDEX index_name ( `imgtype` );
END IF;  
END//  
DELIMITER ;  
CALL schema_change();

 

mysql判断表字段或索引是否存在,然后修改

标签:hot   判断   The   base   com   ati   stat   form   orm   

原文地址:https://www.cnblogs.com/nizuimeiabc1/p/11031086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!