您的当前位置:首页>全部文章>文章详情

Mysql操作多张表的字段,批量新增/修改/删除字段

发表于:2023-09-28 11:44:30浏览:123次TAG: #MySql

多张表同时添加字段

SELECT CONCAT('alter table ',table_schema,'.',table_name,
' add (`create_time` datetime DEFAULT NULL COMMENT "创建时间" ,
  `create_by` varchar(64) DEFAULT NULL COMMENT "创建人" ,
  `update_time` datetime  DEFAULT NULL COMMENT "更新时间" ,
  `update_by` varchar(64) DEFAULT NULL COMMENT "跟新人") ;')  
FROM information_schema.tables  
WHERE table_schema='数据库名' 
AND table_name LIKE 'wms_%';

多张表同时删除字段

SELECT CONCAT('alter table ',table_schema,'.',table_name,
' DROP `create_time` ,
  DROP `create_by` ,
  DROP `update_time` ,
  DROP `update_by`  ;')  
FROM information_schema.tables  
WHERE table_schema='数据库名' 
AND table_name LIKE 'wms_%';

多张表同时更改字段

SELECT CONCAT('alter table ',table_schema,'.',table_name,
' CHANGE `create_time` `createtime` datetime   ,
  CHANGE `create_by` `createby` varchar(128) ;')  
FROM information_schema.tables  
WHERE table_schema='数据库名' 
AND table_name LIKE 'wms_%';

执行上面的操作后我们可以拿到批量SQL语句,然后复制黏贴就可以。