mysql判断表字段或索引是否存在,然后修改
Posted nizuimeiabc1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql判断表字段或索引是否存在,然后修改相关的知识,希望对你有一定的参考价值。
判断字段是否存在:
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判断表字段或索引是否存在,然后修改的主要内容,如果未能解决你的问题,请参考以下文章
Oracle 判断表或字段是否存在新增/修改表结构可重复执行sql