在增加列前进行判断该列是否存在
 
Mysql没有直接的语法可以在增加列前进行判断该列是否存在,需要写一个存储过程完成同样任务,
下面例子是:在T_DT_HOMEWORK表中增加一列RECOMMEND_RECORD列 
 
1
create procedure add_col_homework() BEGIN 
2
IF EXISTS (SELECT column_name FROM information_schema.columns WHERE column_name = 'T_DT_HOMEWORK' AND column_name = 'RECOMMEND_RECORD')
3
THEN 
4
   ALTER TABLE `T_DT_HOMEWORK`  DROP COLUMN `RECOMMEND_RECORD`;
5
END 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`;
7
END;
8

9
drop procedure if exists add_col_homework;