问题描述
实际的日常开发工作中,经常需要对现有表的结构作出变更,涉及到sql相关的操作,基本都通过初始化脚本来完成,如果初始化脚本运行失败或者遇到其他问题,可能导致sql部分执行,不分失败的问题,从而造成这个error 1060 (42s21): duplicate column的问题

问题的解决
方法一:在初始化脚本中添加判断,对于error 1060 (42s21): duplicate column可以容忍的错误直接跳过继续执行
方法二:通过存储过程来实现:
- 相关sql如下:
drop procedure if exists add_col_b;
delimiter ';;';
create procedure add_col_b() begin
if not exists( select * from information_schema.`columns` where table_name='t2' and column_name='b') then
set names utf8;
alter table t2 add b int default 0 comment '';
end if;
end;;
delimiter ';';
call add_col_b();
drop procedure if exists add_col_b;- 具体操作效果:

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论