mysql判断列是否存在

【虚拟资源买卖网www.cnused.com】源码虚拟资源交易平台,免费开通VIP商户!
吆喝资源网官方客服QQ:787749153官方唯一QQ号码
文字广告位招租      好位置不等人!200元/月

本篇文章将通过存储过程来判断列(字段)是否存在。请看详情。

推荐课程: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();

mysql 判断字段否存在,如果存在就修改字段:

DROP PROCEDURE IF EXISTS proc_tempPro;
if(@count>0) THEN 
 alter table 表名 change column `旧列名` `新列名` varchar(30) comment '字段说明';
end if;
end;
call proc_tempPro;
DROP PROCEDURE IF EXISTS proc_tempPro;

通过存储过程判断字段是否存在,不存在则增加:

DROP PROCEDURE IF EXISTS pro_AddColumn;
CREATE PROCEDURE pro_AddColumn() BEGIN
IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='component' AND COLUMN_NAME='PRINT_CHECK_STATUS') THEN
ALTER TABLE component ADD PRINT_CHECK_STATUS int(10) default 0;
END IF;
IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='component' AND COLUMN_NAME='PRINT_CHECK_TIME') THEN
ALTER TABLE component ADD PRINT_CHECK_TIME datetime NULL;
END IF;
IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema=podcloud AND table_name='component' AND COLUMN_NAME='PRINT_CHECK_BACK_REASON') THEN
ALTER TABLE component ADD PRINT_CHECK_BACK_REASON varchar(500) default null;
END IF;
END;
CALL pro_AddColumn;
DROP PROCEDURE pro_AddColumn;

------------------------------------------------------------------------------------------------
DROP PROCEDURE IF EXISTS pro_AddIndex;  
 DELIMITER;
 CREATE PROCEDURE pro_AddIndex() BEGIN 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 pro_AddIndex();
 Drop procedure pro_AddIndex;

以上就是mysql判断列是否存在的详细内容,更多请关注吆喝资源网其它相关文章!

本文转自PHP中文网,吆喝网(http://yaoohe.com)搜集整理,如有疑问请联系本站客服!

本文来源网络,其内容均为发表者提供,文章内容系作者个人观点,不代表本站对观点赞同或支持。如需有侵权,请在7日内联系本站客服进行处理,否则视为放弃追究权利,客服QQ:787749153,请注明。
1

发表评论