MYSQL批量生成数据库语句给所有表添加一列(已经存在的忽略)

文章封面
摘要: MYSQL批量生成数据库语句给所有表添加一列:SELECT CONCAT('alter table ',a.table_schema,'.',a.table_name,' ADD COLUMN `&l

MYSQL批量生成数据库语句给所有表添加一列:

SELECT CONCAT('alter table ',a.table_schema,'.',a.table_name,' ADD COLUMN `<需要添加的表字段名称>` varchar(30) COMMENT "更新时间" NULL DEFAULT NULL ; ')
from information_schema.tables a
where a.table_schema='<数据库名称>'
and (select count(1) from information_schema.`COLUMNS` c where c.table_name = a.table_name
and c.column_name = '需要添加的表字段名称' and a.table_schema='<数据库名称>' and c.table_schema='<数据库名称>') = 0;

MYSQL批量生成数据库删除所有表某个字段语句:

SELECT CONCAT('alter table ',a.table_schema,'.',a.table_name,' drop column <字段名>; ') from information_schema.tables a
where a.table_schema='<数据库名称>'
and (select count(1) from information_schema.`COLUMNS` c where c.table_name = a.table_name
and c.column_name = '<字段名>' and a.table_schema='<数据库名称>' and c.table_schema='<数据库名称>') > 0;

4195 阅读 ← 返回技术栈
图片放大