需要用程序,你可以 select * from test ,或者说 desc test; 然后检查是否存在这个字段,然后根据判断结果进行 alter table test add column adb int;
这个可能得用存储过程来实现: delimiter // set @count = (select count(*) from information_schema.COLUMNS where TABLE_SCHEMA='db' and TABLE_NAME='test' and COLUMN_NAME='abc');if @count > 0 then alter table test add abc int default null; end if; //
不能,需要用存储过程。你可以通过 select 1 from information_schema.columns where TABLE_NAME='test' and TABLE_SCHEMA='db1' and COLUMN_NAME='abc' 来判断是否存在这个字段。
delimiter //
set @count = (select count(*) from information_schema.COLUMNS where TABLE_SCHEMA='db' and TABLE_NAME='test' and COLUMN_NAME='abc');if @count > 0 then
alter table test add abc int default null;
end if;
//