【MySQL】根据字段名关联更新数据库表字段注释

记录一次关联更新字段注释的SQL...

MYSQL本身没有这种批量的命令。不过你可以自己生成 alter table t modify 的脚本,一次性把所有需要修改的列的注释更新。

先利用sql进行表格的信息查询来制作执行语句(网上轮子):

SELECT CONCAT(
               'alter table ',
               table_schema, '.', table_name,
               ' modify column ', column_name, ' ', column_type, ' ',
               IF(is_nullable = 'YES', ' ', 'not null '),
               IF(column_default IS NULL, '',
                  IF(
                              data_type IN ('char', 'varchar')
                              OR
                              data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
                              CONCAT(' default ''', column_default, ''''),
                              CONCAT(' default ', column_default)
                      )
                   ),
               IF(extra IS NULL OR extra = '', '', CONCAT(' ', extra)),
               ' comment ''', column_comment, ''';'
           ) '组合语句'
  FROM information_schema.columns
 WHERE table_schema = 'study'              -- 库名
   AND table_name = 'jsl_bond_info_temp_t' -- 表名
生成的SQL ↓↓↓

alter table study.jsl_bond_info_temp_t modify column put_price decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column turnover_rt decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column curr_iss_amt decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column rating_cd varchar(10) comment '';
alter table study.jsl_bond_info_temp_t modify column issuer_rating_cd varchar(10) comment '';
alter table study.jsl_bond_info_temp_t modify column guarantor varchar(240) comment '';
alter table study.jsl_bond_info_temp_t modify column repo_cd varchar(30) comment '';
alter table study.jsl_bond_info_temp_t modify column sincrease_rt varchar(10) comment '';
alter table study.jsl_bond_info_temp_t modify column premium_rt varchar(10) comment '';
alter table study.jsl_bond_info_temp_t modify column year_left decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column ytm_rt varchar(10) comment '';
alter table study.jsl_bond_info_temp_t modify column increase_rt varchar(10) comment '';
alter table study.jsl_bond_info_temp_t modify column volume decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column short_maturity_dt date comment '';
alter table study.jsl_bond_info_temp_t modify column dblow decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column force_redeem_price decimal(15,3) comment '';
alter table study.jsl_bond_info_temp_t modify column put_convert_price varchar(20) comment '';
alter table study.jsl_bond_info_temp_t modify column convert_amt_ratio varchar(10) comment '';

自己再加注释。。。。

原文地址https://www.cnblogs.com/bufuzhou/p/14683330.html将上面SQL稍微的进行修改

SELECT CONCAT(`组合语句`,'''',
(select 
column_comment `字段说明` 
from information_schema.columns
where table_schema = 'test'		-- 来源库名
and table_name = 'table1' 			-- 来源表名
and COLUMN_NAME=t.column_name 
LIMIT 1
),''';')`修改注释` from (
SELECT CONCAT(
               'alter table `',
               table_schema, '`.`', table_name,'`',
               ' modify column ', column_name, ' ', column_type, ' ',
               IF(is_nullable = 'YES', ' ', 'not null '),
               IF(column_default IS NULL, '',
                  IF(
                              data_type IN ('char', 'varchar')
                              OR
                              data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
                              CONCAT(' default ''', column_default, ''''),
                              CONCAT(' default ', column_default)
                      )
                   ),
               IF(extra IS NULL OR extra = '', '', CONCAT(' ', extra)),
               ' comment ' 
           ) `组合语句`,column_name
  FROM information_schema.columns
 WHERE table_schema = 'test'  	-- 要修改注释的库名
   AND table_name = 'table2' 	 -- 要修改注释的表名
)t 

根据上面SQL查出更新注释说明的SQL,没有的字段会返回null.

替换掉库名和表名就可以使用.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值