Maison > Article > base de données > mysql在增加列前进行判断该列是否存在_MySQL
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;