多张表批量添加单一字段

在平常的数据库操作中,我们通常可能会遇到一些多张数据库表插入同一个字段的问题,这时候如果一张一张的去修改就非常繁琐,小猿在网上查询相关资料后,发现了一个比较好用的方法,下面来看看这个方法具体操作。

SELECT CONCAT('alter table ',table_schema,'.',table_name,' ADD COLUMN `bank_code` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ; ')  
from information_schema.tables  
where table_schema='database_xxx' 
and table_name like 'ti_mybank_%';

information_schema------>所有数据库名
table_schema------>所要操作的数据库名称
table_name------->所要操作的数据库表名
执行上面的操作后我们可以拿到批量修改语句。
在这里插入图片描述
复制后贴入执行即可。
在这里插入图片描述

多张表改变同样的多个字段

按照上述方法我们也可以完成对不同表相同的多个字段的相关操作

添加字段

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='feitian_wms' 
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='feitian_wms' 
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='feitian_wms' 
AND table_name LIKE 'wms_%';
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐