场景
项目中使用MySql数据库,经常要提供一些SQL脚本给用户。所以修改表结构时经常会有ALTER TABLE语句产生。想着如果增加、删除字段时能够先判断字段是否存在,那将会更加方便。
解决方案
create table student (
id bigint(20) primary key,
name varchar(100) not null comment '姓名'
);
-- 增加字段时判断字段是否不存在
alter table student add column if not exists age int(10) comment '年龄';
-- 删除字段时判断字段是否存在
alter table student drop column if exists age;
本文介绍了一种在MySQL数据库中增加或删除表字段时,如何先判断字段是否存在的方法。通过示例展示了如何使用ALTER TABLE语句来实现这一功能。
1034

被折叠的 条评论
为什么被折叠?



