Mysql有没有语法可以在增加列前进行判断该列是否存在

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

 

drop procedure if exists schema_change;
delimiter ';;';
create procedure schema_change() begin
if exists (select * from information_schema.columns where table_name = 'sales_order' and column_name = 'has_sent') then
        alter table sales_order drop column has_sent;
end if;
alter table sales_order add column has_sent boolean;
end;;
delimiter ';';
call schema_change();
drop procedure if exists schema_change;

 

 

 

posted @ 2014-05-21 22:15  IT少年  阅读(1132)  评论(0编辑  收藏  举报