Home  >  Article  >  Database  >  mysql在增加列前进行判断该列是否存在_MySQL

mysql在增加列前进行判断该列是否存在_MySQL

WBOY
WBOYOriginal
2016-06-01 13:28:341364browse

bitsCN.com

mysql在增加列前进行判断该列是否存在

 

Mysql没有直接的语法可以在增加列前进行判断该列是否存在,需要写一个存储过程完成同样任务,

下面例子是:在T_DT_HOMEWORK表中增加一列RECOMMEND_RECORD列 

 

1create procedure add_col_homework() BEGIN 2IF EXISTS (SELECT column_name FROM information_schema.columns WHERE column_name = 'T_DT_HOMEWORK' AND column_name = 'RECOMMEND_RECORD')3THEN 4   ALTER TABLE `T_DT_HOMEWORK`  DROP COLUMN `RECOMMEND_RECORD`;5END IF; 6   ALTER TABLE `T_DT_HOMEWORK` ADD COLUMN  `RECOMMEND_RECORD`  varchar(20) CHARACTER   SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL AFTER `RECOMMEND_ORG_CODE`;7END;89drop procedure if exists add_col_homework;

 


bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn