判斷字段是否存在:
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判斷表字段或索引是否存在的內容就給大家介紹到這裡,希望對大家有所幫助!