Mysql determines whether a table field or index exists

  • 2021-01-22 05:31:43
  • OfStack

Check if the field exists:


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 ' Whether to upload   Whether to upload ';
END IF; 
END// 
DELIMITER ; 
CALL schema_change(); 

Check if the index exists:


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

You can see a lot from these two paragraphs, but you can try it out for yourself

About this site to introduce the Mysql table field or index to determine the existence of the content will be introduced here, I hope to help you!


Related articles: