首页 > 代码库 > mysql 判断表字段或索引是否存在,然后修改
mysql 判断表字段或索引是否存在,然后修改
判断字段是否存在:
1 DROP PROCEDURE IF EXISTS schema_change; 2 DELIMITER // 3 CREATE PROCEDURE schema_change() BEGIN 4 DECLARE CurrentDatabase VARCHAR(100); 5 SELECT DATABASE() INTO CurrentDatabase; 6 IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name = ‘rtc_order‘ AND column_name = ‘IfUpSend‘) THEN 7 ALTER TABLE rtc_order 8 ADD COLUMN `IfUpSend` BIT NOT NULL DEFAULT 0 COMMENT ‘是否上传 是否上传‘; 9 END IF; 10 END// 11 DELIMITER ; 12 CALL schema_change();
判断索引是否存在:
1 DROP PROCEDURE IF EXISTS schema_change; 2 DELIMITER // 3 CREATE PROCEDURE schema_change() BEGIN 4 DECLARE CurrentDatabase VARCHAR(100); 5 SELECT DATABASE() INTO CurrentDatabase; 6 IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = ‘rtc_phototype‘ AND index_name = ‘index_name‘) THEN 7 ALTER TABLE `rtc_Phototype` ADD INDEX index_name ( `imgtype` ); 8 END IF; 9 END// 10 DELIMITER ; 11 CALL schema_change();
mysql 判断表字段或索引是否存在,然后修改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。