Mysql 使用存储过程添加新字段

-- 1, 注意SQL 语句开始处不要空格
-- 2, 在使用 [--] 进行注释时,后面请加空格
USE `test`;

-- lastUpdateTime
drop procedure if exists schema_change;
delimiter ';;';
create procedure schema_change() begin
if exists (select * from information_schema.columns where table_name = 't_my_table' and column_name = 'lastUpdateTime') then
        alter table t_my_table drop column lastUpdateTime;
end if;
alter table t_my_table add column lastUpdateTime  datetime DEFAULT NULL;
end;;
delimiter ';';
call schema_change();
-- myScore 
drop procedure if exists schema_change;
delimiter ';;';
create procedure schema_change() begin
if exists (select * from information_schema.columns where table_name = 't_my_table' and column_name = 'myScore') then
        alter table t_my_table drop column myScore;
end if;
alter table t_my_table add column myScore int(11) DEFAULT '0';
end;;
delimiter ';';
call schema_change();
drop procedure if exists schema_change;

 

posted @ 2015-09-16 10:20  er_bao  阅读(1211)  评论(0编辑  收藏  举报