快活林资源网 Design By www.csstdc.com

判断字段是否存在:

DROP PROCEDURE IF EXISTS schema_change; 
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN 
DECLARE CurrentDatabase VARCHAR();
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 COMMENT '是否上传 是否上传';
END IF; 
END// 
DELIMITER ; 
CALL schema_change(); 

判断索引是否存在:

DROP PROCEDURE IF EXISTS schema_change; 
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN 
DECLARE CurrentDatabase VARCHAR();
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判断表字段或索引是否存在的内容就给大家介绍到这里,希望对大家有所帮助!

快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com