[升级需要用到的] mysql更新表 增加、删除、修改表字段

530 阅读1分钟

1、添加表字段

alter table tab1 add field_name varchar(10) default null comment 'xxx';

alter table tab1 add id int unsigned not null auto_increment primary key;

alter table tab1 add (field1 varchar(10), field2 varchar(20));

2、修改表字段

alter table tab1 modify column field_name; //修改一个字段的类型

alter table user CHANGE new1 new4 int;  //修改一个字段的名称,此时一定要重新指定该字段的类型

3、删除表字段

alter table tab1 drop column field_name;

4、修改表名

rename table old_tabname to new_tabname;

5、检查表字段 待补充... PS 检查表字段是否存在,存在就添加字段, 不存在不处理

6、遍历表字段

pragma table_info(old_tablename)

其中 old_tablename 是表格名,要遍历的表,另外需要存在 table_info 模型类 PS 遍历表所有存在字段,用于升级前检查

参考来源: blog.csdn.net/spirit_8023…