Analyzing mysql table field or index exists, then modifying

Determine whether the field is present:

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
     the ADD  the COLUMN `IfUpSend` the BIT   the NOT  NULL   the DEFAULT  0 the COMMENT ' whether to upload whether to upload ' ;
 the END  the IF ;  
 the END //  
DELIMITER ;  
CALL schema_change();

 

Determine whether the index exists:

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();

 

Guess you like

Origin www.cnblogs.com/nizuimeiabc1/p/11031086.html