1、删除字段
1.1、不作字段是否存在的判断
ALTER TABLE `ssdds2`.`medicine` DROP COLUMN `med_price` ;
1.2、判断字段是否存在
SET @sch = 'ssdds2';SET @tab = 'medicine';SET @col = 'med_price'; delimiter // CREATE PROCEDURE tmp_drop_column(IN S VARCHAR(512),IN T VARCHAR(512),IN C VARCHAR(512)) BEGIN DECLARE tC varchar(200); DECLARE tS varchar(200); SET tC = NULL; SELECT column_name INTO tC FROM information_schema.`COLUMNS` WHERE table_schema = S AND table_name = T AND column_name = C; IF !ISNULL(tC) THEN SET tS = `CONCAT`('ALTER TABLE `',S,'`.`',T,'` DROP COLUMN ',C); SET @t = tS; PREPARE tE from @t; EXECUTE tE; DEALLOCATE PREPARE tE; END IF; END; // delimiter ';' CALL tmp_drop_column(@sch,@tab,@col); DROP PROCEDURE tmp_drop_column;
2、添加字段
2.1、不作字段是否存在的判断
ALTER TABLE `ssdds`.`medicine` ADD COLUMN `med_price` varchar(30) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL COMMENT '药品价格' AFTER `url_picture`;
2.2、判断字段是否存在
SET @sch = 'ssdds2';SET @tab = 'medicine';SET @col = 'med_price'; SET @par = "varchar(30) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL COMMENT '药品价格' AFTER `url_picture`;"; delimiter // CREATE PROCEDURE tmp_add_column(IN S VARCHAR(512),IN T VARCHAR(512),IN C VARCHAR(512),IN P VARCHAR(512)) BEGIN DECLARE tC varchar(200); DECLARE tS varchar(200); SET tC = NULL; SELECT column_name INTO tC FROM information_schema.`COLUMNS` WHERE table_schema = S AND table_name = T AND column_name = C; IF ISNULL(tC) THEN SET tS = `CONCAT`('ALTER TABLE `',S,'`.`',T,'` ADD COLUMN ',C,' ',P); SET @t = tS; PREPARE tE from @t; EXECUTE tE; DEALLOCATE PREPARE tE; END IF; END; // delimiter ';' CALL tmp_add_column(@sch,@tab,@col,@par); DROP PROCEDURE tmp_add_column;