千家信息网

MySQL追加注释或者大量修改注释的方法

发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,这篇文章主要讲解了"MySQL追加注释或者大量修改注释的方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"MySQL追加注释或者大量修改注释的方法"吧
千家信息网最后更新 2024年09月21日MySQL追加注释或者大量修改注释的方法

这篇文章主要讲解了"MySQL追加注释或者大量修改注释的方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"MySQL追加注释或者大量修改注释的方法"吧!

MySQL 5.6.14

之前一个项目比较仓促,开发给的建表语句没有注释.
现在要补全注释信息.
但是MySQL后期追加注释比较麻烦
需要使用modify语法。

只要不小心写错一点,就可能导致表结构的变更,而不是注释的变更.

实验表如下:

  1. create table t(

  2. c1 int primary key auto_increment,

  3. c2 char(20) not null default 'c2' comment 'c2的注释',

  4. c3 date default '2016-01-25' comment 'date类型测试',

  5. c4 varchar(20) not null default '' ,

  6. c5 bigint ,

  7. c6 text comment 'text测试',

  8. c7 timestamp not null default on update not null default now()

  9. );



通过如下的SQL,解析元数据信息,可以直接显示modify的内容.
追加或者修改注释之后,执行语句即可.
这样可以避免人为的失误.

  1. SELECT

  2. concat(

  3. 'alter table ',

  4. table_schema, '.', table_name,

  5. ' modify column ', column_name, ' ', column_type, ' ',

  6. if(is_nullable = 'YES', ' ', 'not null '),

  7. if(column_default IS NULL, '',

  8. if(

  9. data_type IN ('char', 'varchar')

  10. OR

  11. data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',

  12. concat(' default ''', column_default,''''),

  13. concat(' default ', column_default)

  14. )

  15. ),

  16. if(extra is null or extra='','',concat(' ',extra)),

  17. ' comment ''', column_comment, ''';'

  18. ) s

  19. FROM information_schema.columns

  20. WHERE table_schema = 'test'

  21. AND table_name = 't'



以实验表为例,生成的modify语句如下.

  1. alter table test.t modify column c1 int(11) not null auto_increment comment '';

  2. alter table test.t modify column c2 char(20) not null default 'c2' comment 'c2的注释';

  3. alter table test.t modify column c3 date default '2016-01-25' comment 'date类型测试';

  4. alter table test.t modify column c4 varchar(20) not null default '' comment '';

  5. alter table test.t modify column c5 bigint(20) comment '';

  6. alter table test.t modify column c6 text comment 'text测试';

  7. alter table test.t modify column c7 timestamp not null default on update '';

  8. alter table test.t modify column c8 datetime not null default '';

感谢各位的阅读,以上就是"MySQL追加注释或者大量修改注释的方法"的内容了,经过本文的学习后,相信大家对MySQL追加注释或者大量修改注释的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0