资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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追加注释或者大量修改注释的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


名称栏目:MySQL追加注释或者大量修改注释的方法
分享路径:http://cdkjz.cn/article/iicijj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220